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
This logic will first convert the items in list to string(str)
. Sometimes the list contains a tuple like
alist = [(i12,tiger),
(113,lion)]
This logic will write to file each tuple in a new line. We can later use eval
while loading each tuple when reading the file:
outfile = open('outfile.txt', 'w') # open a file in write mode
for item in list_to_persistence: # iterate over the list items
outfile.write(str(item) + '\n') # write to the file
outfile.close() # close the file