简体   繁体   中英

Python file.write() to write backspace to a file

How can i write \\b to a file using the function file.write() in python

Tried file.write('\\b\\b\\b') but it only adds junk characters to my file

 for path in data['paths']:
     file.write(path['method'] + '*')
     file.write(path['url'] + '*')
     for resources in path['resources']:
         file.write(resources['key'])
         file.write('\n'+'**')
     file.write('\b\b\b')

Expect to clear the last instance of ** written from the line file.write('\\n'+'**')

Don't write the \\n** in the first place if you might want to delete them later.

 for path in data['paths']:
     file.write(path['method'] + '*')
     file.write(path['url'] + '*')
     resources = '\n**'.join(x['key'] for x in path['resources'])
     if resources:
         file.write(resources)

Starting with PYthon 3.8, you'll be able to combine the assignment and the if statement:

if resources := '\n**'.join(...):
    file.write(resources)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM