模块-文本变量-configparser
python configparser模块
ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用在程序员写死,可以使程序更灵活
注意:在python 3 中ConfigParser模块名已更名为configparser
configparser函数常用方法:
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类型举例如下:
代码如下:
运行结果如下:
*综合使用方法:*
Last updated