configparser2dict

opthandler.configparser2dict(config, sections=None, create_missing_secs=False, convert=False, **kwargs)[source]

Create a dictionary from a ConfigParser.

Parameters:
  • config (configparser.ConfigParser) – The ConfigParser instance from which to create the dictionary.

  • sections (iterable or str or None, optional) – The sections of config to use for creating the dictionary. If None, use all sections.

  • create_missing_secs (bool, optional) – If True, 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) – If True, apply convert_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 to convert_str().

Returns:

options (dict of dict) – A dictionary containing the entries of the input ConfigParser 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'}}