я так делал
import ConfigParser as cp
def read_ini(filename, defaults):
"""
import ConfigParser as cp
params:
filename: str # config_file.ini
defaults: dict # uses for defaults and type-definition
result:
results: dict # results
structure of defaults and results:
{"section": {"option": typed_value,..} ,..}
"""
if os.path.isfile(filename):
config = cp.RawConfigParser()
config.read(filename)
dict_types = {int: config.getint, float: config.getfloat,
bool: config.getboolean,}
for section in defaults:
if config.has_section(section):
dict_section = defaults[section]
for option in dict_section:
if config.has_option(section, option):
try:
dict_section[option] = \
dict_types.get( \
type(dict_section[option]), config.get) \
(section, option)
except ValueError:
pass
return defaults
defaults = {'main': {'number_size': 8, 'start_position': 4, 'latency': 25,
'finish_position': 7},
}
read_ini(path_to_ini, defaults)