8

I collected data in the form of list of lists and wrote the data into a text file. The data in the text file looks like

[[123231,2345,888754],[223467,85645]]

I want to read it back and store in a list of lists in my program. But when I do read() from the file and try creating a flat list then it takes everything as a string and the interpretation changes totally and i am not able to query the result I get after reading as normal list of lists in python.

Can someone help me with reading the file and storing in the same format as list of lists?

Thank you!

1
  • Can you make your question more clean? Commented Apr 24, 2012 at 8:30

4 Answers 4

9

This looks like valid JSON.

So you can simply do:

import json
with open(myfilename) as f:
    lst = json.load(f)

To store your "list of lists" in a file, do

with open(myfilename, "w") as f:
    json.dump(lst, f)
Sign up to request clarification or add additional context in comments.

5 Comments

How is this a valid JSON? There is no double quotes. There is no key value pair. Can you please explain?
@PranavNandan: Why should there be? Not all JSON objects are key/value pairs. json.loads("[[123231,2345,888754],[223467,85645]]") returns [[123231,2345,888754],[223467,85645]].
See en.wikipedia.org/wiki/JSON#Data_types,_syntax_and_example, specifically the part about arrays.
I tried with the suggested command, and got error " raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 3 (char 2)" any recommendations?
@user17302: Did you use the input from the question or some other (if so, can you share it)?
2

You could just pickle your data instead. To save it:

>>> import pickle
>>> p= [[123231,2345,888754],[223467,85645]]  
>>> with open("data.txt", "wb") as internal_filename:
...     pickle.dump(p, internal_filename)

To load it:

>>> with open("data.txt", "rb") as new_filename:
...     pp = pickle.load(new_filename)
>>> pp
[[123231, 2345, 888754], [223467, 85645]]

This is also useful for much more elaborate objects.

Comments

2

Also you can use eval to achieve this, but the other solutions maybe better:

# reading a list from a file
f = open('data.txt', 'r')
l = eval(f.read())

# storing a list to a file
outf = open('out','w')
outf.write(str(l))

Sometime using eval is a bad practice. For more information check this post.

Comments

2

Make sure that:

  • the file that the list data is in is a python file
  • the list data is assigned to a variable
  • the file is in a python module

You can then import the list from the python file. For example say that list, my_list, is in the file mylist.py

E.g:

# This is the file mylist.py
my_list = [[123231,2345,888754],[223467,85645]]

You can then treat my_list as a python list

from mylist import my_list

for item in mylist:
   print "item = <", item, ">"

1 Comment

This solution was perfect for handling tag value lists in Rockwell Compactlogix .l5k exports. I needed to pull data from a list of list where the initial list was 500+ items, and each item had 30 sub items, and most sub items were lists. My attempts at manually parsing from a text file worked, but had to be fine tuned per list. It had 20+ lines of code. This method uses 1 line! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.