In Python:
Let’s say I have a loop, during each cycle of which I produce a list with the following format:
[‘n1′,’n2′,’n3’]
After each cycle I would like to write to append the produced entry to a file (which contains all the outputs from the previous cycles). How can I do that?
Also, is there a way to make a list whose entries are the outputs of this cycle? i.e.
[[],[],[]] where each internal []=[‘n1′,’n2’,’n3] etc
Writing single list as a line to file
Surely you can write it into a file like, after converting it to string:
Writing all lines at once
When it comes to the second part of your question:
it is also pretty basic. Assuming you want to save it all at once, just write:
Better way of writing a list to file
Depending on what you need, you should probably use one of the more specialized formats, than just a text file. Instead of writing list representations (which are okay, but not ideal), you could use eg.
csvmodule (similar to Excel’s spreadsheet): http://docs.python.org/3.3/library/csv.html