Krcevina
Krcevina

Reputation: 141

config parser: Choosing name and value delimiter

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?

Upvotes: 3

Views: 3693

Answers (1)

Krcevina
Krcevina

Reputation: 141

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

Upvotes: 5

Related Questions