Search code examples
pythonconfigparser

ConfigParser breaking when section header itself has an ]


I am using the ConfigParser module like this:

from ConfigParser import ConfigParser
c = ConfigParser()
c.read("pymzq.ini")

However, the sections gets botched up like this:

>>> c.sections()
['pyzmq:platform.architecture()[0']

for the pymzq.ini file which has ] in tht title to mean something:

[pyzmq:platform.architecture()[0] == '64bit']
url = ${pkgserver:fullurl}/pyzmq/pyzmq-2.2.0-py2.7-linux-x86_64.egg

Solution

  • Looks like ConfigParser uses a regex that only parses section lines up to the first closing bracket, so this is as expected.

    You should be able to subclass ConfigParser/RawConfigParser and change that regexp to something that better suits your case, such as ^\[(?P<header>.+)\]$, maybe.