如何使用python中的file.write()函数将\ b写入文件
尝试过file.write('\ b \ b \ b'),但只会将垃圾字符添加到我的文件中
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')
期望清除从file.write('\n'+'**')
行写入的**的最后一个实例
答案 0 :(得分:2)
如果您以后想删除它们,请不要首先写\n**
。
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)
从PYthon 3.8开始,您将可以将赋值和if
语句组合在一起:
if resources := '\n**'.join(...):
file.write(resources)