0

I am trying to import CONFIG details from config.ini file using python. I am able to get all details into dictionary. But, dictionary contains [DEFAULT] values too. How do I exclude DEFAULT configurations from inserting into my dictionary.

Python code:

config = configparser.ConfigParser()
config.read("C:/config.ini")

dictionary = {}
for section in config.sections():
    dictionary[section] = {}
    for option in config.options(section):
            print(section, option, config.get(section, option))
            dictionary[section][option] = config.get(section, option)

Config File: Need to exclude CONCEPT_REFERENCE and SCHEME from inserting.

[DEFAULT]
CONCEPT_REFERENCE = http://www.xbrl.org/2003/arcrole/concept-reference
SCHEME = http://xbrl.org/entity/identification/scheme

[SQL]
SERVER_NAME = USER\MSSQLSERVER01
DATABASE = MYDB

[NAME]
path_to_log_file = C:/logs/
path_to_output_files = C:/Map/
user2961127
  • 963
  • 2
  • 17
  • 29
  • You can see the answer by @cfi in the following link https://stackoverflow.com/questions/2189288/how-to-exclude-defaults-from-python-configparser-items – CHINTAN VADGAMA Nov 25 '19 at 21:14
  • Does this answer your question? [How to exclude DEFAULTs from Python ConfigParser .items()?](https://stackoverflow.com/questions/2189288/how-to-exclude-defaults-from-python-configparser-items) – CHINTAN VADGAMA Nov 25 '19 at 21:15
  • I went through the mentioned posts but i am unable to resolve my issue – user2961127 Nov 25 '19 at 21:17

1 Answers1

0

With the help from the answer

import configparser
from configparser import NoSectionError
from pprint import pprint
class ConfigParser(configparser.ConfigParser):
    """Can get options() without defaults
    """
    def options(self, section, no_defaults=False, **kwargs):
        if no_defaults:
            try:
                return list(self._sections[section].keys())
            except KeyError:
                raise NoSectionError(section)
        else:
            return super().options(section, **kwargs)

config = ConfigParser()
config.read("test.ini")
dictionary = {}
for section in config.sections():
        dictionary[section] = {}
        for option in config.options(section,no_defaults=True):
                dictionary[section][option] = config.get(section, option)

>>> {'NAME': {'path_to_log_file': 'C:/Arelle-master/arelle/plugin/ferc/data_migration/logs/',
          'path_to_output_files': 'C:/Arelle-master/arelle/plugin/ferc/data_migration/DATA_Map/'},
     'SQL': {'database': 'MYDB', 'server_name': 'USER\\MSSQLSERVER01'}}

In case, you want to include [DEFAULT] section attributes in the dictionary just don't pass no_default=True in the loop.

config = ConfigParser()
config.read("test.ini")
dictionary = {}
for section in config.sections():
        dictionary[section] = {}
        for option in config.options(section):
                # print(section, option, config.get(section, option))
                dictionary[section][option] = config.get(section, option)
pprint(dictionary)
{'NAME': {'concept_reference': 'http://www.xbrl.org/2003/arcrole/concept-reference',
          'path_to_log_file': 'C:/Arelle-master/arelle/plugin/ferc/data_migration/logs/',
          'path_to_output_files': 'C:/Arelle-master/arelle/plugin/ferc/data_migration/DATA_Map/',
          'scheme': 'http://xbrl.org/entity/identification/scheme'},
 'SQL': {'concept_reference': 'http://www.xbrl.org/2003/arcrole/concept-reference',
         'database': 'MYDB',
         'scheme': 'http://xbrl.org/entity/identification/scheme',
         'server_name': 'USER\\MSSQLSERVER01'}}

CHINTAN VADGAMA
  • 634
  • 7
  • 13