1、说明:
python3使用configparser模块来处理ini配置文件。
2、代码示例:
需要生成conf.ini配置文件如下:
[config]
v1 = 100
v2 = abc
v3 = true
v4 = 123.45
python代码:
import configparser
# 加载现有配置文件
conf = configparser.ConfigParser()
# 写入配置文件
conf.add_section('config') #添加section
# 添加值
conf.set('config', 'v1', '100')
conf.set('config', 'v2', 'abc')
conf.set('config', 'v3', 'true')
conf.set('config', 'v4', '123.45')
# 写入文件
with open('conf.ini', 'w') as fw:
conf.write(fw)
# 读取配置信息
v1 = conf.getint('config', 'v1')
v2 = conf.get('config', 'v2')
v3 = conf.ge