[Tutor] string to list
Kent Johnson
kent37 at tds.net
Wed Feb 10 13:51:48 CET 2010
On Wed, Feb 10, 2010 at 6:26 AM, Owain Clarke <simbobo at cooptel.net> wrote:
> Please excuse the obviousness of my question (if it is), but I have searched
> the documentation for how to generate a list e.g. [(1,2), (3,4)] from a
> string "[(1,2), (3,4)]". I wonder if someone could point me in the right
> direction.
Python 2.6 and later contain the function ast.literal_eval() which is
the safe way to do this.
http://docs.python.org/library/ast.html#ast.literal_eval
In [15]: import ast
In [16]: ast.literal_eval("[(1,2), (3,4)]")
Out[16]: [(1, 2), (3, 4)]
Kent
More information about the Tutor
mailing list