把这个configparser使用写以这里方便以后查找使用:
这是ConfigParser.conf里的初始内容:
[mysql]
db_host = 127.0.0.1 db_port = 3306 db_user = root db_pass = password [个人信息] name = 飞飞 age = 30 address = 重庆沙坪坝 tel = 13900000000 [add] add1 = 添加字符串 add2 = 再添加字符串 [del] del1 = 删除字符串 del2 = 删除字符串2 [dele] dele1 = dele1的值 dele2 = dele2的值下面是python3中configparser的处理原码:
# coding:utf-8import configparsercf = configparser.ConfigParser()cf.read("ConfigParser.conf")#返回所有的sections = cf.sections()print("section:",s)print("*" * 70)o1 = cf.options("mysql")print("options:mysql",o1)o2 = cf.options("个人信息")print("options:个人信息",o2)o3 = cf.options("add")print("options:add",o3)o4 = cf.options("del")print("options:del",o4)print("*" * 70)v1 = cf.items("mysql")print("items:mysql",v1)v2 = cf.items("个人信息")print("items:个人信息",v2)v3 = cf.items("add")print("items:add",v3)v4 = cf.items("del")print("items:del",v4)print("*" * 70)db_host = cf.get("mysql","db_host")db_port = cf.getint("mysql","db_port")db_user = cf.get("mysql","db_user")db_pass = cf.get("mysql","db_pass")my_name = cf.get("个人信息","name")my_age = cf.get("个人信息","age")my_address = cf.get("个人信息","address")my_tel = cf.get("个人信息","tel")print("db_host:",db_host)print("db_port:",db_port)print("db_user:",db_user)print("db_pass:",db_pass)print("")print("my_name:",my_name)print("my_age:",my_age)print("my_address:",my_address)print("my_tel:",my_tel)print("*" * 60)#添加[add]#cf.add_section("addd")#cf.write(open("ConfigParser.conf","w"))if cf.has_section("addd"): print("有了 [addd] 了!")else : print("没有 [addd] !!现在开始写入:") cf.add_section("addd") cf.set("addd","addd1","addd1的值") cf.set("addd","addd2","addd2的值") cf.write(open("ConfigParser.conf","w")) print("[addd] 写入完成")#删除一个option#cf.remove_opetion("del","del2")#cf.write(open("ConfigParser.conf","w"))if cf.has_option("del","del2"): print("有del2,现在开始删除:") cf.remove_option("del","del2"); cf.write(open("ConfigParser.conf","w")) print(" [del]下的 del2 已经被删除!")else : print(" [del]下没有 del2了")#删除section[dele]#cf.remove_section("dele")#cf.write(open("ConfigParser.conf","w"))if cf.has_section("dele"): print("有 [dele] 了,开始删除:") cf.remove_section("dele") cf.write(open("ConfigParser.conf","w")) print("删除 [dele] 成功")else: print("没有[dele],不需处理!")#修改一个option#使用set()和新增加一样,这里不再写了#cf.set("section","option","新值")
处理完成后的结果:
[mysql]
db_host = 127.0.0.1 db_port = 3306 db_user = root db_pass = password [个人信息] name = 飞飞 age = 30 address = 重庆沙坪坝 tel = 13900000000 [add] add1 = 添加字符串 add2 = 再添加字符串 [del] del1 = 删除字符串 [addd] addd1 = addd1的值 addd2 = addd2的值说明:
在configparser中 要使用configparser则要先构造一个configparser()然后再读取某个配置文件, import configparser cf = configparser.ConfigParser() cf.read("ConfigParser.conf") 返回所有的section的列表: cf.sections() 返回某个[aaa] section的所有options的列表:cf.options("aaa") 而cf.iteams("aaa")返回所有option的键值组成元组的列表 [aaa] 称为一个section 而[aaa]下的 aaa1=aaa1的值,则称为一个option, 他们可以用has_section("aaa")和has_option("aaa","aaa1")来测试他们是否存在, 添加一个section用add_section("bbb") 添加[bbb]下的一个option为set("bbb","bbb1","bbb1的值"),修改也是使用他来修改, 删除一个section用remove_section("bbb"),注意删除[bbb]后,bbb下所有的option都没有了。 删除[bbb]下的bbb1项用remove_option("bbb","bbb1") 如果是修改了配置文件别忘了写回去write(open("ConfigParser.conf","w"))