Writing a list to a file with Python

后端 未结 21 1838
孤街浪徒
孤街浪徒 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 02:07

    Let avg be the list, then:

    In [29]: a = n.array((avg))
    In [31]: a.tofile('avgpoints.dat',sep='\n',dtype = '%f')
    

    You can use %e or %s depending on your requirement.

提交回复
热议问题