修改INI文件Python中节的参数值(Modify parameter values of sections in INI files Python)

编程入门 行业动态 更新时间:2024-10-27 13:22:02
修改INI文件Python中节的参数值(Modify parameter values of sections in INI files Python)

我有一个.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)

I have a .ini file which I want to modify a particular section. So for example,

[Section1] Param1: Hello Param2: World [Section2] fontsize = 10 [Section3] integers = 971 508 1076 561

I want to modify the integers in section 3 and replace them with other integers. I have tried:

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

But I'm getting errors; how can I fix this?

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)

You're missing the section and field to change the values of, try with:

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

Keep in mind that after changing the values you still need to save your config:

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

更多推荐

本文发布于:2023-07-31 00:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   文件   Python   INI   Modify

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!