Update INI file without removing comments

2019-03-24 06:22发布

问题:

Consider the following INI file:

[TestSettings]
# First comment goes here
environment = test

[Browser]
# Second comment goes here
browser = chrome
chromedriver = default

...

I'm using Python 2.7 to update the ini file:

config = ConfigParser.ConfigParser()
config.read(path_to_ini)
config.set('TestSettings','environment',r'some_other_value')

with open(path_to_ini, 'wb') as configfile:
    config.write(configfile)

How can I update the INI file without removing the comments. The INI file is updated but the comments are removed.

[TestSettings]
environment = some_other_value

[Browser]
browser = chrome
chromedriver = default

回答1:

ConfigObj preserves comments when reading and writing INI files, and seems to do what you want. Example usage for the scenario you describe :

from configobj import ConfigObj

config = ConfigObj(path_to_ini)
config['TestSettings']['environment'] = 'some_other_value'
config.write()


回答2:

The reason that comments in config files are wiped when writing back is that the write method didn't take care of comments at all. It just writes key/value pairs.

The easiest way to bypass this is to init configparser object with a customized comment prefix and allow_no_value = True. If we want to keep the default "#" and ";" comment lines in the file, we can use comment_prefixes='/'.

i.e., to keep comments, you have to trick configparser into believing this is not a comment, this line is a key without a value. Interesting :)

# set comment_prefixes to a string which you will not use in the config file
config = configparser.ConfigParser(comment_prefixes='/', allow_no_value=True)
config.read_file(open('example.ini'))
...
config.write(open('example.ini', 'w'))


回答3:

ConfigObj is the best option in almost all cases.

Nevertheless, it does not support multiline values without triple quotes, like ConfigParser do. In this case, a viable option can be iniparse.

For example:

[TestSettings]
# First comment goes here
multiline_option = [
        first line,
        second line,
    ]

You can update the multiline value in this way.

import iniparse
import sys

c = iniparse.ConfigParser()
c.read('config.ini')
value = """[
    still the first line,
    still the second line,
]
"""
c.set('TestSettings', 'multiline_option', value=value)
c.write(sys.stdout)