views:

26

answers:

1

According to the documentation:

The configuration file consists of sections, led by a [section] header and followed by name: value entries, with continuations in the style of RFC 822 (see section 3.1.1, “LONG HEADER FIELDS”); name=value is also accepted. Python Docs

However, writing a config file always use the equal sign (=). Is there any option to use the colon sign (:)?

Thanks in advance.

H

+2  A: 

If you look at the code defining the RawConfigParser.write method inside ConfigParser.py you'll see that the equal signs are hard-coded. So to change the behavior you could subclass the ConfigParser you wish to use:

import ConfigParser
class MyConfigParser(ConfigParser.ConfigParser):
    def write(self, fp):
        """Write an .ini-format representation of the configuration state."""
        if self._defaults:
            fp.write("[%s]\n" % DEFAULTSECT)
            for (key, value) in self._defaults.items():
                fp.write("%s : %s\n" % (key, str(value).replace('\n', '\n\t')))
            fp.write("\n")
        for section in self._sections:
            fp.write("[%s]\n" % section)
            for (key, value) in self._sections[section].items():
                if key != "__name__":
                    fp.write("%s : %s\n" %
                             (key, str(value).replace('\n', '\n\t')))
            fp.write("\n")

filename='/tmp/testconfig'    
with open(filename,'w') as f:
    parser=MyConfigParser()
    parser.add_section('test')
    parser.set('test','option','Spam spam spam!')
    parser.set('test','more options',"Really? I can't believe it's not butter!")
    parser.write(f)

yields:

[test]
more options : Really? I can't believe it's not butter!
option : Spam spam spam!
unutbu