考虑以下INI文件:
[TestSettings]
# First comment goes here
environment = test
[Browser]
# Second comment goes here
browser = chrome
chromedriver = default
...
我正在使用Python 2.7更新ini文件:
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)
如何在不删除注释的情况下更新INI文件。 INI文件已更新,但注释已被删除。
[TestSettings]
environment = some_other_value
[Browser]
browser = chrome
chromedriver = default
ConfigParser
来完成这个任务,你需要使用其他的库。 - David Heffernan