Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #56230
| Date | 2013-10-05 20:24 -0500 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.com> |
| Subject | Re: how to read list from file |
| References | <fbc6e512-88fa-4de0-80d3-6757fcc52af4@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.767.1381022585.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to read list from file Harvey Greenberg <hjgreenberg@gmail.com> - 2013-10-05 18:08 -0700
Re: how to read list from file Tim Chase <python.list@tim.thechases.com> - 2013-10-05 20:24 -0500
Re: how to read list from file Roy Smith <roy@panix.com> - 2013-10-05 21:35 -0400
Re: how to read list from file Harvey Greenberg <hjgreenberg@gmail.com> - 2013-10-06 09:41 -0700
Re: how to read list from file Harvey Greenberg <hjgreenberg@gmail.com> - 2013-10-06 09:46 -0700
Re: how to read list from file Ravi Sahni <ganeshsahni07@gmail.com> - 2013-10-06 22:20 +0530
Re: how to read list from file Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-07 09:04 +0300
Re: how to read list from file Roy Smith <roy@panix.com> - 2013-10-05 21:34 -0400
Re: how to read list from file Terry Reedy <tjreedy@udel.edu> - 2013-10-06 02:24 -0400
Re: how to read list from file Harvey Greenberg <hjgreenberg@gmail.com> - 2013-10-06 09:57 -0700
Re: how to read list from file Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-06 18:24 +0100
Re: how to read list from file Ravi Sahni <ganeshsahni07@gmail.com> - 2013-10-06 23:08 +0530
csiph-web