Back
Is this the cleanest way to write a list to a file, since writelines() doesn't insert newline characters?
file.writelines(["%s\n" % item for item in list])
It seems like there would be a standard way.
For writing a list to a file with Python you can use a loop:-
with open('your_file.txt', 'w') as f: for item in my_list: f.write("%s\n" % item)
with open('your_file.txt', 'w') as f:
for item in my_list:
f.write("%s\n" % item)
If you are using Python 2, you can also use the following piece of code:-
with open('your_file.txt', 'w') as f: for item in my_list: print
print
31k questions
32.8k answers
501 comments
693 users