本文来自 :https://www.cnblogs.com/yang1333/articles/12609714.html#3177870913html
Copy[section1] k1 = v1 k2:v2 # configparser配置文件也能够使用":"冒号, 关联option及对应的值 user=egon age=18 is_admin=true salary=31 [section2] k1 = v1
Copyimport configparser """ # 由于python2的模块名命名的规范没有统一, 因此在python2中导入方式是: import ConfigParser # 基于这种模块的运用, 文件的后缀名结尾通常命名成2种格式: 文件.ini 或者 文件.cfg """ config = configparser.ConfigParser() config.read('a.cfg') # 查看全部的标题: 获取文件中全部的"sections". 返回值一个列表类型, 列表中的元素。对应着每个section。 print(config.sections()) # ['section1', 'section2'] # 查看标题section1下全部key=value的key: 获取文件中对应的"section1"下全部的"options"。返回值一个列表类型, 列表中的元素。对应着当前"section"中的每个option print(config.options('section1')) # ['k1', 'k2', 'user', 'age', 'is_admin', 'salary'] # 查看标题section1下全部key=value的(key,value)格式: 返回相似于这种格式[(option1, value1), (option2, value2)] print(config.items('section1')) # [('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31.1')] # 查看标题section1下user的值: 获取文件中"section1"下的"user"这个options对应的值. 返回的是字符串类型 print(config.get('section1', 'k2')) # v2 res = config.get('section1', 'user') print(res, type(res)) # egon <class 'str'> # 查看标题section1下age的值: 返回整型int类型 res = int(config.get('section1', 'age')) print(res, type(res)) # 18 <class 'int'> res = config.getint('section1', 'age') # 替代上面使用int的方式 print(res, type(res)) # 18 <class 'int'> # 查看标题section1下is_admin的值: 返回布尔值 res = config.getboolean('section1', 'is_admin') print(res, type(res)) # True <class 'bool'> # 查看标题section1下salary的值: 返回浮点型 res = config.getfloat('section1', 'salary') print(res, type(res)) # 31.1 <class 'float'> """
Copyimport configparser config = configparser.ConfigParser() config.read('a.cfg', encoding='utf-8') # 删除整个标题section2 config.remove_section('section2') # 删除标题section1下的某个k1和k2 config.remove_option('section1', 'k1') config.remove_option('section1', 'k2') # 判断是否存在某个标题 print(config.has_section('section1')) # 判断标题section1下是否有user print(config.has_option('section1', '')) # 添加一个标题 config.add_section('egon') # 在标题egon下添加name=egon,age=18的配置 config.set('egon', 'name', 'egon') config.set('egon', 'age', 18) # 报错,必须是字符串 # 最后将修改的内容写入文件,完成最终的修改 config.write(open('a.cfg', 'w'))
Copyimport configparser config = configparser.ConfigParser() config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {} config['bitbucket.org']['User'] = 'hg' config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Host Port'] = '50022' # mutates the parser topsecret['ForwardX11'] = 'no' # same here config['DEFAULT']['ForwardX11'] = 'yes' with open('example.ini', 'w') as configfile: config.write(configfile)