Parsing tuple from string?

George Sakkis george.sakkis at gmail.com
Wed Apr 23 19:22:05 EDT 2008


On Apr 23, 6:24 pm, Daniel <the.d... at gmail.com> wrote:
> I have a list of strings, which I need to convert into tuples.  If the
> string is not in python tuple format (i.e. "('one', 'two')", "("one",
> 'two')", etc.), then I can just make it a 1-tuple (i.e. return
> (string,) ).  If it is in python tuple format, I need to parse it and
> return the appropriate tuple (it's ok to keep all tuple elements as
> strings).
>
> I think eval() will work for this, but I don't know what will be in
> the string, so I don't feel comfortable using that.

Check out one of the safe restricted eval recipes, e.g.
http://preview.tinyurl.com/6h7ous.

HTH,
George



More information about the Python-list mailing list