config parser: Choosing name and value delimiter

冷暖自知 提交于 2019-12-10 22:53:40

问题


Let's say I have one test.ini file with the following lines:

[A]
name1 [0,1]=0
name2 a:b:c / A:B:C [0,1]=1

When I parse it like this:

A = ConfigParser.ConfigParser()
with codecs.open('test.ini', 'r') as f:
    A.optionxform = str
    A.readfp(f)

for section_name in A.sections():
    print 'Section:', section_name
    print 'Options:', A.options(section_name)
    for name, value in A.items(section_name):
        print 'name-value pair:'
        print '%s' % (name)
        print '%s' % (value)

I get the following output:

Section: A
Options: ['name1 [0,1]', 'name2 a']
name-value pair:
name1 [0,1]
0
name-value pair:
name2 a
b:c / A:B:C [0,1]=1

But that is not what I want, I want it to be like this:

Section: A
Options: ['name1 [0,1]', 'name2 a:b:c / A:B:C [0,1]']
name-value pair:
name1 [0,1]
0
name-value pair:
name2 a:b:c / A:B:C [0,1]
1

Is there a way to somehow choose the delimiter between name and value so that it only can be = sign?

And if there are more than just one = in a line, that the delimiter be the last one?


回答1:


Problem solved by skipping to Python 3.3 and: A = configparser.ConfigParser(delimiters=('='))



来源:https://stackoverflow.com/questions/21328509/config-parser-choosing-name-and-value-delimiter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!