ConfigParser
requires all sections, keys and values to be strings; no surprise. It has methods to convert the values to datatypes with getfloat
, getint
, getboolean
. If you don't know the datatype, you can wrap the get()
with an eval()
to get have the string evaluated such as:
>>> from ConfigParser import SafeConfigParser
>>> cp = SafeConfigParser()
>>> cp.add_section('one')
>>> cp.set('one', 'key', '42')
>>> print cp.get('one', 'key')
'42'
>>> print eval(cp.get('one', 'key'))
42
>>> cp.set('one', 'key', 'None')
>>> print eval(cp.get('one', 'key'))
None
>>>
Is there a better way? I assume there some grave security concerns with evaluating text from a file- which I acknowledge; I completely trust the file.
I thought I would use pickle
for this, but I would really like to keep the config file human readable.
How would you do it?