我有一个格式为 example.txt 的文件:
a,b,c,d
我想将其转换为:
a
b
c
d
我写了这个,但它不起作用:
with open('newline.txt', 'w') as f:
f.write (file("example.txt", "r").read().replace(",", "\n"))
我有一个格式为 example.txt 的文件:
a,b,c,d
我想将其转换为:
a
b
c
d
我写了这个,但它不起作用:
with open('newline.txt', 'w') as f:
f.write (file("example.txt", "r").read().replace(",", "\n"))
你也可以这样做:
with open('newline.txt', 'w') as f:
f.writelines (w + '\n' for w in open("example.txt").read().split(","))
它应该是:
with open('newline.txt', 'w') as f:
f.write (open("example.txt", "r").read().replace(",", "\n"))
纪录片中说,打开文件时,最好使用 open() 而不是直接调用 file() 构造函数