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
In Python3 You Can use this loop
with open('your_file.txt', 'w') as f:
for item in list:
f.print("", item)
with open ("test.txt","w")as fp:
for line in list12:
fp.write(line+"\n")
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)
Another way of iterating and adding newline:
for item in items:
filewriter.write(f"{item}" + "\n")
Using numpy.savetxt
is also an option:
import numpy as np
np.savetxt('list.txt', list, delimiter="\n", fmt="%s")
You can also use the print function if you're on python3 as follows.
f = open("myfile.txt","wb")
print(mylist, file=f)