Writing a list to a file with Python

后端 未结 21 1794
孤街浪徒
孤街浪徒 2020-11-22 01:48

Is this the cleanest way to write a list to a file, since writelines() doesn\'t insert newline characters?

file.writelines([\"%s\\n\" % item  fo         


        
21条回答
  •  迷失自我
    2020-11-22 02:21

    In python>3 you can use print and * for argument unpacking:

    with open("fout.txt", "w") as fout:
        print(*my_list, sep="\n", file=fout)
    

提交回复
热议问题