【发布时间】:2018-04-01 12:57:56
【问题描述】:
他们有什么方法可以在 python 中使用 ConfigObj 更新属性文件中的键值。
更新属性文件之前:
hostKey = "value"
更新属性文件后:
hostKey = "updatedValue"
【问题讨论】:
他们有什么方法可以在 python 中使用 ConfigObj 更新属性文件中的键值。
更新属性文件之前:
hostKey = "value"
更新属性文件后:
hostKey = "updatedValue"
【问题讨论】:
来自 ConfigObj 文档
创建一个新的配置文件就像阅读一个文件一样简单。你可以 在创建 ConfigObj 时指定文件名,或者稍后再做 [2]。
如果不设置文件名,那么 write 方法会返回一个列表 行而不是写入文件。更多见写方法 详情。
这里我们展示了创建一个空的 ConfigObj,设置一个文件名和一些 值,然后写入文件:
from configobj import ConfigObj
config = ConfigObj("test.config")
config.filename ="test.config"
config['keyword1'] = "value6"
config['keyword2'] = "value2"
config.write()
我认为类似的方式你可以读取并设置为相同的文件名,然后覆盖你想要的属性。
【讨论】:
config.filename 行,但它应该可以工作。
这应该会有所帮助。
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.
【讨论】: