6

I would like to read a configuration file with the python ConfigParser module:

[asection]
option_a = first_value
option_a = second_value

And I want to be able to get the list of values specified for option 'option_a'. I tried the obvious following:

test = """[asection]
option_a = first_value
option_a = second_value
"""
import ConfigParser, StringIO
f = StringIO.StringIO(test)
parser = ConfigParser.ConfigParser()
parser.readfp(f)
print parser.items()

Which outputs:

[('option_a', 'second_value')]

While I was hoping for:

[('option_a', 'first_value'), ('option_a', 'second_value')]

Or, even better:

[('option_a', ['first_value', 'second_value'])]

Is there a way to do this with ConfigParser ? Another idea ?

mgilson
  • 300,191
  • 65
  • 633
  • 696
mathieu
  • 2,954
  • 2
  • 20
  • 31
  • Instead of doing that, use one of the many ways of storing lists detailed [here](http://stackoverflow.com/questions/335695/lists-in-configparser) – Fredrick Brennan Jan 30 '13 at 12:44
  • The question is not about how to store a list in a config file. It is about how to use ConfigFile to read _that_ file format. From reading http://stackoverflow.com/questions/287757/pythons-configparser-unique-keys-per-section I can see that this is impossible though. – mathieu Jan 30 '13 at 12:48
  • @mathieu 'impossible' to a degree, it can not be done using the method you want to use, but what is more important to you? the actual method used, or the result? the result is attainable, you just need to use the right tool. – Inbar Rose Jan 30 '13 at 14:20
  • Right, I thought it was clear I meant "this is impossible with ConfigParser for that file format" though. What should I do with this question ? write myself an answer that explains the problem ? – mathieu Jan 31 '13 at 08:19
  • Does this answer your question? [Python's ConfigParser unique keys per section](https://stackoverflow.com/questions/287757/pythons-configparser-unique-keys-per-section) – Tomerikoo Oct 21 '20 at 16:43

1 Answers1

1

It seems that it is possible to read multiple options for the same key, this may help: How to ConfigParse a file keeping multiple values for identical keys?

Here is a final code from this link (I tested on python 3.8):

from collections import OrderedDict
from configparser import ConfigParser
class ConfigParserMultiValues(OrderedDict):
    def __setitem__(self, key, value):
        if key in self and isinstance(value, list):
            self[key].extend(value)
        else:
            super().__setitem__(key, value)

    @staticmethod
    def getlist(value):
        return value.splitlines()

config = ConfigParser(strict=False, empty_lines_in_values=False, dict_type=ConfigParserMultiValues, converters={"list": ConfigParserMultiValues.getlist})
config.read(["test.ini"])
values = config.getlist("test", "foo")
print(values)

For test.ini file with content:

[test]
foo = value1
foo = value2
xxx = yyy

The output is:

['value1', 'value2']
Ira
  • 26
  • 2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30602080) – betelgeuse Dec 16 '21 at 09:37
  • Sorry, I'm a newbie...I'll keep in mind next time! – Ira Dec 16 '21 at 12:18