configparser2dict
- opthandler.configparser2dict(config, sections=None, create_missing_secs=False, convert=False, **kwargs)[source]
Create a dictionary from a
ConfigParser
.- Parameters
config (
configparser.ConfigParser
) – TheConfigParser
instance from which to create the dictionary.sections (
iterable
orstr
orNone
, optional) – The sections of config to use for creating the dictionary. IfNone
, use all sections.create_missing_secs (
bool
, optional) – IfTrue
, don’t raise an exception if a given section is not contained in config but instead create a key for this section that holds as value an empty sub-dictionary.convert (
bool
, optional) – IfTrue
, applyconvert_str()
to the values of the returned dictionary. This will convert strings to their corresponding Python data types.kwargs (
dict
, optional) – Keyword arguments to parse toconvert_str()
.
- Returns
options (
dict
ofdict
) – A dictionary containing the entries of the inputConfigParser
as a dictionary of dictionaries. Every section name becomes a key that holds as value a sub-dictionary with the options as keys and the option values as values.
Examples
>>> import configparser >>> config = configparser.ConfigParser() >>> config['Monty'] = {'spam': 'no!', 'eggs': '2'} >>> config['Python'] = {'foo': 'fighter', 'bar': 'baz'} >>> configparser2dict(config) {'Monty': {'spam': 'no!', 'eggs': '2'}, 'Python': {'foo': 'fighter', 'bar': 'baz'}}