paython3-练习

时间:2021-10-03 16:43:08

在文本每行末尾加;

f = open(r'D:\test1\1.txt','rb')
w = open(r'D:\test1\2.txt','wb') for line in f.readlines():
w.writelines(line.replace('\r\n',';\r\n'))
w.close()

更好的写法

f = open(r'D:\test1\1.txt','rb')
w = open(r'D:\test1\2.txt','wb')
import os
with f as lines:
with w as outfile:
for line in lines:
line = '"' + line.replace(os.linesep, "") + '",' + os.linesep
outfile.write(line)
类似写法
f = open(r'D:\test1\1.txt','rb')
w = open(r'D:\test1\2.txt','wb') for line in f.readlines():
line = '"'+ line.replace('\r\n',"") + '";\r\n'
w.writelines(line)
f.close()
w.close()