修改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 561I 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)更多推荐
发布评论