前言
configparser模块提供了ConfigParser类,它实现了一种基本的配置语言,解析类似于Microsoft Windows INI文件的结构。
configparser不会猜测数据类型,一律当做str处理。
使用
通常,ini格式文件的结构如下:
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no
获取ini内容:
import configparser
config = configparser.ConfigParser()
config.read('example.ini', encoding="utf-8")
print(config['bitbucket.org']['user'])
##输出
hg
其中config对section大小不敏感.
可以看出,config类似于dict,可以使用items进行遍历。
■
\blacksquare
■