Reputation: 75
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.
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]
...
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?
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)
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]
Upvotes: 1
Views: 1428
Reputation: 177685
With an input file of:
['some_text','foo', 1]
['some_text','foo', 2]
['some_text','foo', 3]
This will read/edit/write the file:
import ast
with open('input.txt') as f:
data = [ast.literal_eval(line) for line in f]
print(data)
data[0][1] = 'bar'
data[-1][-1] = 5
print(data)
with open('output.txt','w') as f:
for line in data:
f.write(str(line) + '\n')
But writing Python lists as text to a file isn't really the best way to go about it. A better way is to use the csv
module of you are willing to change the input file to this:
some_text,foo,1
some_text,foo,2
some_text,foo,3
And use this code:
import csv
with open('input.txt',newline='') as f:
r = csv.reader(f)
data = list(r)
print(data)
# Note, with csv, all columns are read as strings,
# so to do calculations convert:
data = [[a,b,int(c)] for a,b,c in data]
print(data)
data[0][1] = 'bar'
data[-1][-1] += 2
print(data)
with open('output.txt','w',newline='') as f:
w = csv.writer(f)
w.writerows(data)
Also, look into the pandas
third-party module.
Upvotes: 1