python 写文件换行

#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

    原文作者:一蹬大诗
    原文地址: https://blog.csdn.net/sksvenska/article/details/7671032
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞