I am trying to extract duplicate values of options from a section using configparser. I have searched and tried with the solution provided here. With this, I am able to extract the values of the duplicate keys of a option section. But when I try to extract the name:value pair using the function configparser.items(), I do not get any value.
[server]
unix-user = ivmgr
root = 1
root = 2
root = 3
class MultiOrderedDict(OrderedDict):
def __setitem__(self, key, value):
if isinstance(value, list) and key in self:
self[key].extend(value)
else:
super(OrderedDict, self).__setitem__(key, value)
config = ConfigParser.RawConfigParser(dict_type=MultiOrderedDict)
config.read(['1.conf'])
print config.get("server", "unix-user")
print config.get("server", "root")
['ivmgr']
['1', '2', '3']
>>> print config.items("server")
[]
I want to be able to extract the name:value pair either as a dict or tuples e.g { 'root':'1', 'root':'2', 'root':'3' }. I am using python 2.7. I know I can just create a dict or list of sets using root as the keys for all, but I may be having more number of duplicate keys inside the same section and I want a pythonic way of doing this.
Thank you.
>>>config.sections() [] – chingkhuba May 07 '15 at 18:10