Safe eval, or how to get list from string

Michael Spencer mahs at telcopartners.com
Fri May 13 13:44:04 EDT 2005


bwooster47 at gmail.com wrote:
> I've to use ConfigParser.
> 
> It returns values that are exactly in the config file, so get string
> variables like:
> int1 with quotes and characers: "42"
> this is easy to convert to int:
> realint = int(int1)
> 
> I've read the tutorial, and the FAQ, and not sure if I missed it, but
> other than calling eval (which everyone says is unsafe), I don't know
> if there is another way to do this:
> string1 has "this is a string" or however complex a string (""" aaa
> """).
> I can do
> realstring = eval(string1)
> or
> if there is list1 = "[ 'filea', 'fileb', ]", I can get at list by
> doing:
> reallist = eval(list1)
> 
> is there an easier/simpler method of doing the same thing as realstring
> and reallist lines above?
> 
See: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469

Michael




More information about the Python-list mailing list