How to get back a list object from its string representation?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Dec 31 01:27:01 EST 2008


On Wed, 31 Dec 2008 03:08:29 -0200, Gabriel Genellina wrote:

> eval is like Pandora´s box, all kind of bad things can come from it. Do
> not use it with any user-supplied string. If you can restrict the values
> to be just constants, there is a "safe eval" recipe in
> http://code.activestate.com

The ast module in Python 2.6 includes a "literal eval" function:

>>> ast.literal_eval("[1, 2, 3]")
[1, 2, 3]



-- 
Steven



More information about the Python-list mailing list