2

I'm copying sections and options and values from one .ini file to another in order to merge multiple .ini files into one, with RawConfigParser.

In one source .ini I have this and would like it to be copied:

[foo]
bar=""

However, the result I got is

[foo]
bar=

this fails my requirement because the closed external program I test won't work with this ini.

I've tried with '""', \"\", "\"\"" but without success. (edit: note that in the output '' won't do any good for me either, it needs to be "")

My code is:

import ConfigParser

inireader = ConfigParser.RawConfigParser()
inireader.read('source.ini')

iniwriter=ConfigParser.RawConfigParser()
for section_name in inireader.sections():
    for name, value in inireader.items(section_name):
        print name,value
        if not iniwriter.has_section(section_name):
            iniwriter.add_section(section_name)
        iniwriter.set(section_name, name, value)
with open("output.ini", "wb") as f:
    iniwriter.write(f)

if value == "": iniwriter.set(section_name,name,'""') works, but is this a bug? Or am I doing something wrong? Is there a non-hackish way to do this?

edit: I'm using Python 2.7

n611x007
  • 8,952
  • 8
  • 59
  • 102

1 Answers1

3

From looking at the ConfigParser source code, it looks like there is a special case where the read() function will convert two double quotes as a value to an empty string.

Not exactly sure why that decision was made, but it is definitely intentional behavior and I think that you will need to go with your hacky if value == "": iniwriter.set(section_name,name,'""') to work around this.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • 1
    Hm, you're right. Unexpected from a ***Raw***ConfigParser, and even without an option to turn it off. Thanks for digging it up! – n611x007 Oct 31 '13 at 17:02