I would like to build a list or a list of list by reading it from a text file.
For example, I have a file 'mylist.txt' saying
mylist = [
1,
2,
3,
4]
myotherlist = [
[1, 3, 4, 5],
[3, 5, 3, 6]
]
I would like to read that text file and then use that in my python script.
Is that possible? I just use the above example in python syntax. My goal is to avoid writing parsing logic myself. And XML seems to be too complicated for my purpose.
Thank you.
If I had the following data file (data.txt):-
[1,2,3,4]
Then this code would read it into a list.
from ast import literal_eval
with open('data.txt') as fsock:
mylist = literal_eval( fsock.read() )
This will also work for other datatypes e.g. dictionaries. See literal_eval docs for more details.
If it's a list of lists, then you should just define it "straight up" in your mylists.txt file.
e.g.
MYLISTS = [
[1, 2, 3],
[2, 3, 4],
...
]
And rename mylists.txt to mylists.py.
Then, with another Python script in the same directory, you can just do
import mylists
for sublist in mylists.MYLIST:
for elem in sublist:
....
This requires no parsing code on your part, and the Python "data" file is very easy to hand-edit, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With