can ConfigParser deal with repeating section header?
Tim Chase
python.list at tim.thechases.com
Thu Nov 12 10:59:54 EST 2015
On 2015-11-12 07:47, John Zhao wrote:
> I have a configuration file with repeating sections, for example,
>
> [INSTANCE]
> Name=a
>
> [INSTANCE]
> Name=b
>
> I hope I can use ConfigParser to read the file and store the
> configuration settings in arrays.
>
> Is that possible?
Not with the standard library's ConfigParser:
>>> from cStringIO import StringIO
>>> from ConfigParser import ConfigParser
>>> ini = StringIO("""[INSTANCE]
... Name=a
...
... [INSTANCE]
... Name=b
... """)
>>> cp = ConfigParser()
>>> cp.readfp(ini)
>>> cp.sections() # note: only one section
['INSTANCE']
>>> cp.get("INSTANCE", "Name") # note: only the last value
'b'
It also fails to catch multiple values with the same key:
[INSTANCE]
Name=a
Name=b
doesn't give you
cp.get("INSTANCE", "Name") == ["a", "b"]
-tkc
More information about the Python-list
mailing list