1
votes

I am reading a config file with Python's ConfigParser (Python 3.4). My values are all integers, e.g.

key1 = 11
key2 = 22

When I write the config file, after having read it into a Config object, with

with open('settings.ini', 'w') as configfile:
    Config.write(configfile)

my file ends up looking like this:

key1 = ['11']
key2 = ['22']

Why is the file being written like this, and not as it originally was?

key1 = 11
key2 = 22
2

2 Answers

0
votes

Because you are passing list instead of the list's value?

Your should be setting the value, by something like this:

config.set('section1', 'key1', key_val_variable)

Can you try printing 'key_val_variable'

Please try:

import ConfigParser

config = ConfigParser.ConfigParser()
config.read("settings.ini")

key_var = ['key1']

if not config.has_section('section1'):
   config.add_section('section1')

config.set('section1', 'key1', key_var)
config.set('section1', 'key2', key_var[0])

with open("settings.ini", 'wb') as configfile:
   config.write(configfile)
0
votes

I found my problem. In my long list of 800+ options, I had some duplicate keys, which was corrupting the parsing of the ini file. When I removed the duplicate keys, all worked as expected.