Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top