ConfigParser
2025/5/13...小于 1 分钟
ConfigParser
配置文件解释器
configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近。Python2.x 中名为 ConfigParser,3.x 已更名小写,并加入了一些新功能。
配置文件的格式如下:
ghost.ini
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[main]
chasepos = 1
maxround = 25
resize = 0
doublepointnumper100 = 9
warnminute = 20
[ ]
包含的为 section,section 下面为类似于 key - value 的配置内容;
configparser 默认支持=
、:
两种分隔。
获取指定 key 的 value
# 读取配置文件
conn = ConfigParser()
conn.read('ghost.ini')
# 获取
conn.get('main', 'chasepos')
conn.getint('main', 'chasepos')
conn['main']['chasepos']
参考文档: