3

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Krcevina
  • 131
  • 4
  • 13
  • Okay, I found out that, since I'm using Python 2.7, I need to update ConfigParser module to the newer one https://pypi.python.org/pypi/configparser. Now there should be an option for customizing key(name) and value delimiter... – Krcevina Jan 24 '14 at 15:24

1 Answers1

4

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

Krcevina
  • 131
  • 4
  • 13