Question:
How to write a nested list from a variable into a text file so that each list inside the nested list is written into it's own line?
The list is needed to be compiled back into the text file in exactly the same format as it was before pulling it out with the f.readfiles()
function.
Other than variable based solutions and multi-variable solutions are also accepted. csv module
is also an option but I'd rather learn the logic of this first without it.
Introduction to the problem
Assume there is a text file
that contains list
in each and every line like this:
['some_text','foo', some_int]
['some_text','foo', some_int]
['some_text','foo', some_int]
...
Each list contains several items. All the lines are wanted to be read
from the text file into a one big nested list and stored into a variable
like this:
variable = [['some_text','foo', some_int],
['some_text','foo', some_int],
['some_text','foo', some_int]]
The format of the information stored into the variable above is irrelevant. The list stored into the variable is now edited with some list tools. When editing is complete, the variable containing the edited list is written back to the same text file again, overwriting all the data with new changed data like this:
['some_text','bar', some_int]
['edited_tx','foo', some_int]
['some_text','foo', edtd_int]
...
Reading the lines
I managed to read the lines with this code:
with open("my_lists.txt") as f:
listed_text = f.readlines()
All the text inside the text file is now stored into a variable named 'listed_text' which by then is being edited with some list tools prefered.
More about the code above is here:
How do I read a file line-by-line into a list?
Incomplete code example for reading and writing the file
This is incomplete and inoperative coding example of what should have been done with the lists.
# Let us open 'my_lists.txt' and read lines
# into a variable as a list.
def open_file():
with open("my_lists.txt") as f:
listed_text = f.readlines()
list_item_editor(listed_text)
# Now let us replace 'foo' every index(1) in
# nested lists with word 'bar' with
# this imaginary editor 'list_item_editor()'.
def list_item_editor(argument):
edited_list = []
for item in nested_list:
nested_list[item][i] = "bar"
...
...
write_file(edited_list)
# Now we write the edited list back to 'my_lists.txt'
# by replacing the whole contents with new edited list.
def write_file(arg):
with open("my_lists.txt", "w") as write_file:
write_file.write(arg)
Preferred output
This is example of the preferred output. Each item (list) inside the nested list is written into it's own line. The 'new line mark' \n
is not visible inside text file.
# This is text_file.txt containing edited lists
# New line inserted after every list
['some_text','bar', some_int]
['edited_tx','foo', some_int]
['some_text','foo', edtd_int]
['some_text','bar', some_int]
['edited_tx','foo', some_int]
['some_text','foo', edtd_int]
['some_text','bar', some_int]
['edited_tx','foo', some_int]
['some_text','foo', edtd_int]