Safe eval, or how to get list from string

Edvard Majakari edvard+news at majakari.net
Mon May 16 03:14:13 EDT 2005


bwooster47 at gmail.com writes:

> 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)

There's already a method getint() for that, you can just say

cfgparser.getint('section', 'int1')

> if there is list1 = "[ 'filea', 'fileb', ]", I can get at list by
> doing:
> reallist = eval(list1)

Are you sure you need to use ConfigParser for that? If you need to have
persistent Python data structures, I'd suggest marshalling data and using
(c)Pickle or similar.

-- 
# Edvard Majakari		Software Engineer
# PGP PUBLIC KEY available    	Soli Deo Gloria!

"Debugging is twice as hard as writing the code in the firstplace. Therefore,
 if you write the code as cleverly as possible, you are, by definition,
 not smart enough to debug it."  -- Brian W. Kernighan



More information about the Python-list mailing list