user2080223
user2080223

Reputation:

Getting tuple error when trying to parse config file

I recived a good snipet on howto parse an config file into a dictionary earlyer, but I can't seem to find why it can't parse my config file (since I dont have any tuples outside the comments)

My error msg,

Traceback (most recent call last):   File "test2.py", line 9, in
<module>
    CONFIG_DATA[section_name][item_name] = cfg.get(section_name, item_name)   File "C:\Python27\lib\ConfigParser.py", line 614, in get
    option = self.optionxform(option)   File "C:\Python27\lib\ConfigParser.py", line 374, in optionxform
    return optionstr.lower() AttributeError: 'tuple' object has no attribute 'lower'

The code,

import ConfigParser
from pprint import pprint
cfg = ConfigParser.ConfigParser()
cfg.read('config2.cfg')
CONFIG_DATA = {}
for section_name in cfg.sections():
    CONFIG_DATA[section_name] = {}
    for item_name in cfg.items(section_name):
        CONFIG_DATA[section_name][item_name] = cfg.get(section_name, item_name)
pprint(CONFIG_DATA)

My config file, http://pastebin.com/UKnrXFGR

Upvotes: 0

Views: 852

Answers (1)

kirelagin
kirelagin

Reputation: 13626

ConfigParser.items(section[, raw[, vars]])

Return a list of (name, value) pairs for each option in the given section. Optional arguments have the same meaning as for the get() method.

Either do:

for item_name in cfg.options(section_name): # Note `options`
    CONFIG_DATA[section_name][item_name] = cfg.get(section_name, item_name)

or:

for item_name, item_value in cfg.items(section_name):
    CONFIG_DATA[section_name][item_name] = item_value

Upvotes: 2

Related Questions