#writelines 写入文件不会默认换行
file_path=”c:\\tt\\pyresult.txt”
str_list=[’11’,’22’,’33’]
f=open(file_path, “w”)
f.writelines(str_list)#调用writelines方法
文件结果:
112233
#需要显示的加入换行标记
file_path=”c:\\tt\\pyresult.txt”
str_list=[’11’,’22’,’33’]
f=open(file_path, “w”)
str_list = [line+’\n’ for line in str_list]#在list中加入换行符
f.writelines(str_list)
#或者另一种形式比较好读
file_path=”c:\\tt\\pyresult.txt”
str_list=[’11’,’22’,’33’]
f=open(file_path, “w”)
for line in str_list:
f.write(line)
f.write(‘\n’)#显示写入换行
结果:
11
22
33