how to read list from file
Tim Chase
python.list at tim.thechases.com
Sat Oct 5 21:24:39 EDT 2013
On 2013-10-05 18:08, Harvey Greenberg wrote:
> I am looping as for L in file.readlines(), where file is csv.
>
> L is a list of 3 items, eg, [{'a':1, 'b':2}, [1,2,3], 10] Note that
> the first item is a dir and 2nd is a list, so parsing with split
> doesn't work. Is there a way to convert L, which is a string, to
> the list of 3 items I want?
sounds like you want ast.literal_eval():
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> s = "[{'a':1, 'b':2}, [1,2,3], 10]"
>>> import ast
>>> print repr(ast.literal_eval(s))
[{'a': 1, 'b': 2}, [1, 2, 3], 10]
-tkc
More information about the Python-list
mailing list