python - 修改INI文件中节的参数值Python

标签 python python-3.x ini configparser

我有一个 .ini 文件,我想修改它的特定部分。 例如,

[Section1]
Param1: Hello
Param2: World

[Section2]
fontsize = 10

[Section3]
integers = 971 508 1076 561

我想修改第3节中的整数并用其他整数替换它们。我尝试过:

lis = "971 508 1076 561; 920 543 973 648 ; 831 492 936 544 ; 936 403 988 508"
config.set('sfr',lis)

但是我遇到了错误;我该如何解决这个问题?

import configparser
config = configparser.ConfigParser()
config.read("C:\\Users\\Folder\\example.ini")
print(config.sections())
lis = "971 508 1076 561; 920 543 973 648 ; 831 492 936 544 ; 936 403 988 508"
config.set('sfr',lis)

最佳答案

您缺少要更改其值的部分和字段,请尝试使用:

config.set("Section3", "integers", lis)

请记住,更改值后您仍然需要保存配置:

with open("C:\\Users\\Folder\\example.ini", "w") as f:
    config.write(f)

关于python - 修改INI文件中节的参数值Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42306214/

相关文章:

Python-如何在for循环中将新值与先前值进行比较?

ms-access - 要从 Access 导出的 schema.ini 文件

用于读取 .ini 文件的 Windows 批处理脚本

python - 使用pythonturtle绘制n条边的多边形

python - 设置一个 cookie 并使用 Python 和 WSGI 检索它

python - 关于 appengine 索引的 3 个问题

python - 在类内部创建类的副本

python - 网页抓取 - Python - 在 html 中找不到链接

python-3.x - 合并字典中的两个列表,保留具有多个值的键的重复值

c++ GetPrivateProfileString 从当前目录读取ini文件