ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section)。每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用在程序员写死,可以使程序更灵活。
注意:在python3中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类型
更多详细内容请看:https://www.cnblogs.com/lhly/p/8066898.html