Reputation: 20774
Is there a really easy way to save a list of strings to a file in Python? The strings should be separated by new lines, and the file is to be read by humans, outside of Python.
Ideally, something as simple as numpy.savetxt
for numeric arrays.
Upvotes: 1
Views: 370
Reputation: 369064
Using pickle.dump
:
with open('/path/to/file', 'wb') as f:
pickle.dump(a_list_of_strings, f)
To get the list back, use pickle.load
.
UPDATE for human readable output, use file.writelines
.
Assuming there's no newline is contained in the strings:
with open('/path/to/file', 'w') as f:
f.writelines('%s\n' % s for s in a_list_of_strings)
Upvotes: 1