2014-01-31 130 views
10

考虑下面的INI文件更新INI文件评论。 INI文件已更新,但注释已删除。而不删除评论

[TestSettings] 
environment = some_other_value 

[Browser] 
browser = chrome 
chromedriver = default 
+0

您无法使用'ConfigParser'来执行此操作。您需要使用其他库。 –

+0

你试过了allow_no_value参数吗? http://stackoverflow.com/questions/6620637/python-configparser-question-about-writing-comments-to-files – markcial

+0

allow_no_value对读取配置没有影响。也就是说,评论不是在第一个地方被读出来写下来的...... –

回答

5

ConfigObj保留comments读取和写入时INI文件,并似乎做你想要什么。您所描述场景的示例用法:

from configobj import ConfigObj 

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

诀窍漂亮:) – houcros

+3

'ConfigObj'已过时。看到一个分叉https://github.com/DiffSK/configobj –