我正在使用 ConfigObj(5.0.6,在 python 2.7 和 python 3.8 上)来管理我的配置,但是当我写入文件配置时,某些部分仅在 configspec 中显示,它们仅显示为空部分,其中是不希望的。对于修复 ConfigObj 的行为的任何建议,我将不胜感激。
发生的事情的最小示例:
from configobj import ConfigObj
from validate import Validator
spec = ["[Section]", "option = boolean(default=True)"]
config = ConfigObj(infile={'Section2': {'option2': False}}, configspec=spec)
config.validate(Validator())
print(config)
print(config.write())
输出:
{'Section2': {'option2': False}, 'Section': {'option': True}}
['[Section2]', ' option2 = False', '[Section]']
期望的输出(写的时候不应该有空的部分):
{'Section2': {'option2': False}, 'Section': {'option': True}}
['[Section2]', ' option2 = False']
编辑 1:我正在使用 write() 来实际写入文件,所以我不希望只是弄乱返回的字符串列表