[英]How to update properties file using ConfigObj
Is their any way for updating value of a key in properties file using ConfigObj in python. 它们是使用python中的ConfigObj更新属性文件中键值的任何方法。
Before updating properties file: 在更新属性文件之前:
hostKey = "value"
After updating properties file: 更新属性文件后:
hostKey = "updatedValue"
From ConfigObj Documentation 从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].
您可以在创建ConfigObj时指定文件名,也可以稍后进行[2]。
If you don't set a filename, then the write method will return a list of lines instead of writing to file.
如果未设置文件名,则write方法将返回行列表,而不是写入文件。 See the write method for more details.
有关更多详细信息,请参见write方法。
Here we show creating an empty ConfigObj, setting a filename and some values, and then writing to file :
在这里,我们显示创建一个空的ConfigObj,设置一个文件名和一些值,然后写入文件:
from configobj import ConfigObj
config = ConfigObj("test.config")
config.filename ="test.config"
config['keyword1'] = "value6"
config['keyword2'] = "value2"
config.write()
I think similar way u can read and set to the same file name and then over-write the property you want. 我认为您可以以类似的方式读取并设置为相同的文件名,然后覆盖所需的属性。
This should help. 这应该有所帮助。
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.