Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I build a python datastructure by reading it from a file

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.

like image 663
michael Avatar asked Feb 18 '23 02:02

michael


2 Answers

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.

like image 88
Martin Avatar answered Feb 20 '23 16:02

Martin


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.

like image 25
tom Avatar answered Feb 20 '23 15:02

tom