ConfigParser and $'s in the options

Steffen Ries steffen.ries at
Fri Sep 14 14:12:05 CEST 2001

HariNam Singh <hsingh at> writes:

> I'm pretty new to Python (Java background). It's a pretty cool language so
> far. I was happy to find that there is already code that handles dealing
> with configuration files: ConfigParser.
> Though, I run into an issue with it. It refuses to parse configuration files
> that have $ - signs in the option names. As I'm working on a 3rd party app,
> which I don't have the source code for, I'll have to use the $ signs in the
> option names. Is there a way to make ConfigParser do that, without changing
> the code?

You can subclass ConfigParser and provide your own regular expression
for parsing options:

import ConfigParser
import re

class ConfigParserDollar(ConfigParser.ConfigParser):
    def __init__(self, defaults=None):
        ConfigParser.ConfigParser.__init__(self, defaults)
        self.OPTCRE = re.compile(
            #              added '$'   ^^^
            r'[ \t]*(?P<vi>[:=])[ \t]*'

The value of OPTCRE is copied from the implementation + the '$' as
valid option letter.

Alternatively you can override OPTCRE in your instance of

steffen.ries at	<> Gravity is a myth -- the Earth sucks!

More information about the Python-list mailing list