Python全栈开发:configparser模块
#!/usr/bin/env python # -*- coding;utf-8 -*- import configparser # 创建对象 conn = configparser.ConfigParser() # conn对象具有read功能,打开文件读取文件,放入内存,要保存必须重写 conn.read("ini", encoding="utf-8") # conn对象的sections作用:在内存中寻找所有的[???] result = conn.sections() print(result) # 从指定的[???]中寻找所有的key result = conn.options("section2") print(result) # 获取指定节点下key的值 result = conn.get("section1", "k1") result = conn.getint("section1", "k1") result = conn.getfloat("section1", "k1") result = conn.getboolean("section1", "k1") print(result) # 判断是否存在某个节点 result = conn.has_section("section1") print(result) # 判断某个节点下是否存在key result = conn.has_option("section1", "k1") print(result) # 移除某个节点的键值对 result = conn.remove_option("section1", "k1") conn.write(open("文件名", "w")) # 参数是一个可写的对象 # 设置某个节点的键值对,涉及到中文要明确编码,否则出现乱码 result = conn.set('a', "k5", "你好吗") conn.write(open("好看吗", "w", encoding="utf-8"))