1

There is a matter that I can't resolve in Python.

I'm trying to get lists by reading file (like .xml or .txt).

I've put my lists in a big list in my file like it :

[[48,49,39,7,13,1,11],[46,27,19,15,24,8,4],[35,5,41,10,31,5,9],[12,9,22,2,36,9,2],[50,47,25,6,42,3,1]]

Now I'm looking for code to get this big list like a list, not like a string. In deed, I've already try some parts of code with open(), write() and read() functions. But Python returned me :

'[[48,49,39,7,13,1,11],[46,27,19,15,24,8,4],[35,5,41,10,31,5,9],[12,9,22,2,36,9,2],[50,47,25,6,42,3,1]]'

And it isn't a list, just a string. So I can't use list's functions to modify it.

Thanks for those who will answer to my problem

Templeton Peck
  • 151
  • 1
  • 3
  • 10

4 Answers4

7

well, a simple way is to parse it as a json string:

>>> import json
>>> l_str = '[[48,49,39,7,13,1,11],[46,27,19,15,24,8,4],[35,5,41,10,31,5,9],[12,9,22,2,36,9,2],[50,47,25,6,42,3,1]]'
>>> l = json.loads(l_str)
>>> print l
[[48, 49, 39, 7, 13, 1, 11], [46, 27, 19, 15, 24, 8, 4], [35, 5, 41, 10, 31, 5, 9], [12, 9, 22, 2, 36, 9, 2], [50, 47, 25, 6, 42, 3, 1]]

if you want to load a file that only contains that string, you can simply do it using the following:

>>> import json
>>> with open('myfile') as f:
>>>     l = json.load(f)
>>>     print l
[[48, 49, 39, 7, 13, 1, 11], [46, 27, 19, 15, 24, 8, 4], [35, 5, 41, 10, 31, 5, 9], [12, 9, 22, 2, 36, 9, 2], [50, 47, 25, 6, 42, 3, 1]]

But if what you want is to serialize python objects, then you should instead use pickle that's more powerful at that task…

Of course, there are other ways that others may give you to parse your string through an eval()-like function, but I strongly advice you against that, as this is dangerous and leads to insecure code. Edit: after reading @kamikai answer, I'm discovering about ast.literal_eval() which looks like a decent option as well, though json.loads() is more efficient.

Community
  • 1
  • 1
zmo
  • 24,463
  • 4
  • 54
  • 90
1

If your example is truly representative of your data (i.e., your text file contains only a list of lists of integers), you can parse it as JSON:

import json
data = read_the_contents_of_the_file()
decoded = json.loads(data)

Replace data = read_the_contents_of_the_file() with your existing code for reading the contents as string.

lanzz
  • 42,060
  • 10
  • 89
  • 98
1

As seen here, the inbuilt ast module is probably your best bet, assuming the text is still valid python.

import ast

ast.literal_eval("[[1,2,3], [4,5,6], [7,8,9]]") # Returns nested lists
Kamikai
  • 185
  • 1
  • 6
0

Use json to load and parse the file:

import json
with open(my_file_path, "rb") as f:
    my_list = json.load(my_file_path)
tmr232
  • 1,171
  • 14
  • 23