1. 简介
configparser用于配置文件解析,可以解析特定格式的配置文件,多数此类配置文件名格式为XXX.ini,例如mysql的配置文件。在python3.X中
模块名为configparser ,在python2.X中使用的模块名为ConfigParser。
##### ini 文件示例 ######## [section1] name = wang age = 18 [section2] name:python age = 19 #### 文件格式说明 ######### [XXX] 代表节点 XX = XX 或者 XX : XX 代表参数
read(filename) #读取配置文件,直接读取ini文件内容 sections() #获取ini文件内所有的section,以列表形式返回['logging', 'mysql'] options(sections) #获取指定sections下所有options ,以列表形式返回['host', 'port', 'user', 'password'] items(sections) #获取指定section下所有的键值对,[('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')] get(section, option) #获取section中option的值,返回为string类型 >>>>>获取指定的section下的option <class 'str'> 127.0.0.1 getint(section,option) 返回int类型 getfloat(section, option) 返回float类型 getboolean(section,option) 返回boolen类型