使用ConfigParser Python更改ini文件中的值 [英] Change value in ini file using ConfigParser Python
本文介绍了使用ConfigParser Python更改ini文件中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我有这个settings.ini:
So, I have this settings.ini :
[SETTINGS]
value = 1
还有这个python脚本
And this python script
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('settings.ini')
print parser.get('SETTINGS', 'value')
如您所见,我想读取然后将值"1"替换为另一个.到目前为止,我所能做的就是阅读它.我在网上搜索了如何替换它,但没有找到.
As you can see, I want to read and then replace the value "1" by another one. All I was able to do so far is to read it. I searched on the net how to replace it but I didn't find.
推荐答案
根据文档示例:
https://docs.python.org/2/library/configparser.html
parser.set('SETTINGS', 'value', '15')
# Writing our configuration file to 'example.ini'
with open('example.ini', 'wb') as configfile:
parser.write(configfile)
这篇关于使用ConfigParser Python更改ini文件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文