ConfigParser [英] ConfigParser
问题描述
问候。
由于CongiParser文件中的部分由[和]分隔,为什么
没有用于转义的转义(和unescape)函数
&,[和]字符到& amp;,& lbrack;和& rbrack; ?
感谢Manlio Perillo
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
推荐答案
2004年11月10日星期三10:39 :27 GMT,Manlio Perillo
< NO ****************** @ libero.it>写道:
On Wed, 10 Nov 2004 10:39:27 GMT, Manlio Perillo
<NO******************@libero.it> wrote:
问候。
由于CongiParser文件中的部分由[和]分隔,为什么
没有转义(和unescape)用于将&,[和]字符转义为& amp;,& lbrack的功能;和& rbrack; ?
感谢Manlio Perillo
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
只是来自ConfigParser的子类并添加了自己的功能
类似:
===在这里切割===
来自ConfigParser import ConfigParser
class MyConfigParser(ConfigParser):
def get(self,section,option,raw = False,vars = None):#ilay
超级的get()方法
" ;"" get(section,option,[raw = False],[vars = None]) - >字符串"""
repl = [(''& lbrack;'',''[''),
('& rbrack;' ','''''),
]#extent as
就像你喜欢的那样
t = ConfigParser.get(self,section,选项,原始,变量)#调用
获得超级
for x,y in repl:t = t.replace(x,y)#do thebr />
替换东西
返回t
if __name __ ==" __ main __":
"" ;"test""
import os
open(''test。
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 '')。写(''[test] \ nopt1 = Hello& lbrack; Ivo
Woltring& rbrack; \ n'')
ini = MyConfigParser( )
ini.read(''test。
'',''w'').write(''[test]\nopt1=Hello[Ivo
Woltring]\n'')
ini = MyConfigParser()
ini.read(''test.
'')
print ini.get(''test'', ''opt1'')
os.unlink(''test。
'')
print ini.get(''test'', ''opt1'')
os.unlink(''test.
这篇关于ConfigParser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!