3

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.

Community
  • 1
  • 1
chingkhuba
  • 41
  • 1
  • 4
  • I suspect it comes from config = ConfigParser.RawConfigParser(dict_type=MultiOrderedDict) Could you try a config.sections () ? – Alvein May 07 '15 at 08:34
  • Ok. I get below >>>config.sections() ['server'] However if I go with config = ConfigParser.RawConfigParser(), I get
    >>>config.sections() []
    – chingkhuba May 07 '15 at 18:10

2 Answers2

2

In Python 3.6.0, the dict type provides keys method can make the method sections working as normal. Currently I don't know why the redefinition is required here. The MultiOrderedDict looks like:

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)
    def keys(self):
        return super(OrderedDict, self).keys()
maxshuty
  • 9,708
  • 13
  • 64
  • 77
robin
  • 51
  • 7
0

Getting results in dictionary is not possible because of that your key is always 'root' and it is going to replace the previous value. You can use a simple trick instead of forcing to change the default functionality of ConfigParser:

results = [('root', v) for v in config.get("server",  "root")]

However, maybe it is better to change the settings. I do not know why you did not set a tuple as a value to 'root' in your settings file:

[server]
unix-user = ivmgr
root = (1, 2, 3)

This is working for me.

import ConfigParser
class M(dict):
    def __setitem__(self, key, value):
        if key in self:
            items = self[key]
            new = value[0]
            if new not in items:
                items.append(new)
        else:
            super(M, self).__setitem__(key, value)
cp = ConfigParser.ConfigParser(dict_type=M)
cp.read('x.settings')
print cp.get('server', 'root')
results = [('root', v) for v in cp.get("server",  "root")]
print results
  • Thanks Abi, but I still get the last value only, >>>[('root', v) for v in config.get("server", "root")] [('root', '3')] The settings file is a well known config file of a server and has the format already mentioned the way it is. I can't change it, as the server uses it, in the tuple format you mentioned, it would not be recognized by the server. – chingkhuba May 07 '15 at 18:22
  • 1
    I added a code that worked for me. However, I do not thinks so it is a good way :) – Abi M.Sangarab May 11 '15 at 13:45