Python – Configuration parser: Choosing name and value delimiter

Configuration parser: Choosing name and value delimiter… here is a solution to the problem.

Configuration parser: Choosing name and value delimiter

Let’s say I have a test.ini file that contains the following line:

[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’s not what I want,
I hope it looks 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
Can there only be an = sign between name and value?

If there is more than one = in a row,
Is the separator the last?

Solution

The problem is solved by jumping to Python 3.3 and: A = configparser. ConfigParser(delimiters=('='))

Related Problems and Solutions