I need to be able to use the ConfigParser
to read multiple values for the same key. Example config file:
[test]
foo = value1
foo = value2
xxx = yyy
With the 'standard' use of ConfigParser
there will be one key foo
with the value value2
. But I need the parser to read in both values.
Following an entry on duplicate key I have created the following example code:
from collections import OrderedDict
from ConfigParser import RawConfigParser
class OrderedMultisetDict(OrderedDict):
def __setitem__(self, key, value):
try:
item = self.__getitem__(key)
except KeyError:
super(OrderedMultisetDict, self).__setitem__(key, value)
return
print "item: ", item, value
if isinstance(value, list):
item.extend(value)
else:
item.append(value)
super(OrderedMultisetDict, self).__setitem__(key, item)
config = RawConfigParser(dict_type = OrderedDict)
config.read(["test.cfg"])
print config.get("test", "foo")
print config.get("test", "xxx")
config2 = RawConfigParser(dict_type = OrderedMultisetDict)
config2.read(["test.cfg"])
print config2.get("test", "foo")
print config.get("test", "xxx")
The first part (with config
) reads in the config file us 'usual', leaving only value2
as the value for foo
(overwriting/deleting the other value) and I get the following, expected output:
value2
yyy
The second part (config2
) uses my approach to append multiple values to a list, but the output instead is
['value1', 'value2', 'value1
value2']
['yyy', 'yyy']
How do I get rid of the repetitive values? I am expecting an output as follows:
['value1', 'value2']
yyy
or
['value1', 'value2']
['yyy']
(I don't mind if EVERY value is in a list...). Any suggestions welcome.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…