ConfigParser
Ivo Woltring
Python at IvoNet.nl
Wed Nov 10 09:39:42 EST 2004
On Wed, 10 Nov 2004 10:39:27 GMT, Manlio Perillo
<NOmanlio_perilloSPAM at libero.it> wrote:
>Regards.
>
>Since sections in CongiParser files are delimited by [ and ], why
>there is not an escape (and unescape) function for escaping
>&, [, and ] characters to &, [ and ] ?
>
>
>
>
>Thanks Manlio Perillo
just subclass from ConfigParser and add the functionality youself
somthing like:
=== cut here ===
from ConfigParser import ConfigParser
class MyConfigParser(ConfigParser):
def get(self, section, option, raw=False, vars=None): # override
the get() method of super
"""get(section, option, [raw=False], [vars=None]) --> String"""
repl = [('[','['),
(']',']'),
] # extent as
much as U like
t = ConfigParser.get(self, section, option, raw, vars) # call the
get of super
for x,y in repl: t=t.replace(x,y) # do the
replace stuff
return t
if __name__=="__main__":
"""test"""
import os
open('test.$$$','w').write('[test]\nopt1=Hello[Ivo
Woltring]\n')
ini = MyConfigParser()
ini.read('test.$$$')
print ini.get('test', 'opt1')
os.unlink('test.$$$')
=== End Cut ===
you can ofcourse do this in reverse for the write function so as not
to have to write the [ etc. yourself.
Cheerz,
Ivo Woltring
More information about the Python-list
mailing list