https://stackoverflow.com/questions/17225287/python-2-7-write-and-read-a-list-from-file
To write:
with open(the_filename, 'wb') as f:
pickle.dump(my_list, f)
To read:
with open(the_filename, 'rb') as f:
my_list = pickle.load(f)
If you do need them to be human-readable, we need more information.
If
my_list
is guaranteed to be a list of strings with no embedded newlines, just write them one per line:with open(the_filename, 'w') as f:
for s in my_list:
f.write(s + '\n')
with open(the_filename, 'r') as f:
my_list = [line.rstrip('\n') for line in f]
Comments
Post a Comment