Read all the contents in ini file into dictionary with Python

前端 未结 8 1908
天涯浪人
天涯浪人 2020-12-04 22:28

Normally, I code as follows for getting a particular item in a variable as follows

try:
    config = ConfigParser.ConfigParser()
    config.read(self.iniPath         


        
8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 23:01

    I managed to get an answer, but I expect there should be a better one.

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

提交回复
热议问题