如何使用 ConfigObj 更新属性文件

How to update properties file using ConfigObj

他们是否有任何方法可以使用 python 中的 ConfigObj 更新属性文件中的键值。

更新属性文件之前:

hostKey = "value"

更新属性文件后:

hostKey = "updatedValue"

来自 ConfigObj 文档

Creating a new config file is just as easy as reading one. You can specify a filename when you create the ConfigObj, or do it later [2].

If you don’t set a filename, then the write method will return a list of lines instead of writing to file. See the write method for more details.

Here we show creating an empty ConfigObj, setting a filename and some values, and then writing to file :

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

我认为您可以通过类似的方式读取并设置为相同的文件名,然后 over-write 您想要的 属性。

这应该有所帮助。

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.