0

他们是在 python 中使用 ConfigObj 更新属性文件中键值的任何方式吗?

在更新属性文件之前:

hostKey = "value"

更新属性文件后:

hostKey = "updatedValue"
4

2 回答 2

2

来自 ConfigObj 文档

创建一个新的配置文件就像阅读一个文件一样简单。您可以在创建 ConfigObj 时指定文件名,或者稍后再执行 [2]。

如果您没有设置文件名,那么 write 方法将返回行列表而不是写入文件。有关详细信息,请参阅 write 方法。

这里我们展示了创建一个空的 ConfigObj,设置一个文件名和一些值,然后写入文件:

from configobj import ConfigObj
config = ConfigObj("test.config")
config.filename ="test.config"
config['keyword1'] = "value6"
config['keyword2'] = "value2"
config.write()

我认为类似的方式你可以读取并设置为相同的文件名,然后覆盖你想要的属性。

于 2018-04-01T13:01:33.403 回答
2

这应该会有所帮助。

from configobj import ConfigObj
config = ConfigObj("FileName")

print(config['hostKey'])
config['hostKey'] = "updatedValue"        #Update Key
config.write()                            #Write Content
print(config['hostKey'])                  #Check Updated value. 
于 2018-04-01T13:06:22.443 回答