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.
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)
with open('~/Desktop/test', 'w') as f: pickle.dump(['a','b'], f) the written file contains gibberish: (lp0 S'a' p1 aS'b' p2 a. The file should be human-readable.