python - 使用 ConfigParser Python 更改 ini 文件中的值

标签 python ini configparser

所以,我有这个 settings.ini :

[SETTINGS]

value = 1

还有这个 python 脚本

from ConfigParser import SafeConfigParser

parser = SafeConfigParser()
parser.read('settings.ini')

print parser.get('SETTINGS', 'value')

如您所见,我想读取值“1”,然后将其替换为另一个值。到目前为止,我所能做的就是阅读它。我在网上搜索如何更换它,但没有找到。

最佳答案

来自文档的示例:

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)

关于python - 使用 ConfigParser Python 更改 ini 文件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27964134/

相关文章:

python - 将数据分箱到基于非网格的箱中

用于 INI 文件读取器的 C++ 自定义枚举结构

php - php中ini_set()的目的是什么? (尤其是错误报告)

wix - 如何使用 Wix 工具集检查 ini 文件是否存在

python - 读取多个配置文件时,ConfigParser 会覆盖之前的文件,为什么?

python - 在 Python 中模拟方法调用

python - 如何以有效的方式基于 pandas 中的其他行更新行

python - 如何删除文本之间的单个空格

python - 在Python中运行sql server查询时出现错误

python - 读取配置文件时出现 ConfigParser.MissingSectionHeaderError Python