如何使用Python configparser模块的常用方法进行配置文件解析?

2026-05-25 00:171阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计694个文字,预计阅读时间需要3分钟。

如何使用Python configparser模块的常用方法进行配置文件解析?

%E2%80%9CConfigParser%E6%A8%A1%E5%9D%97%E5%9C%A8Python%E4%B8%AD%E7%94%A8%E6%9D%A5%E8%AF%BB%E5%8F%96%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E3%80%82%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E6%98%AF%E4%B8%80%E7%A7%8D%E6%A0%BC%E5%BC%8F%EF%BC%8C%E5%8F%AF%E4%BB%A5%E5%8C%85%E5%90%AB%E5%A4%9A%E4%B8%AA%E8%8A%82%E3%80%82%E6%AF%8F%E4%B8%AA%E8%8A%82%E5%8F%AF%E4%BB%A5%E5%8C%85%E5%90%AB%E5%A4%9A%E4%B8%AA%E5%8F%82%E6%95%B0%E3%80%82%E2%80%9D

ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用在程序员写死,可以使程序更灵活。

注意:在python 3 中ConfigParser模块名已更名为configparser

configparser函数常用方法:

读取配置文件:

read(filename) #读取配置文件,直接读取ini文件内容

sections() #获取ini文件内所有的section,以列表形式返回['logging', 'mysql']

如何使用Python configparser模块的常用方法进行配置文件解析?

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类型

举例如下:

配置文件ini如下:

[logging]
level = 20
path =
server =

[mysql]
host=127.0.0.1
port=3306
user=root
password=123456

注意,也可以使用:替换=

代码如下:

import configparser from until.file_system import get_init_path conf = configparser.ConfigParser() file_path = get_init_path() print('file_path :',file_path) conf.read(file_path) sections = conf.sections() print('获取配置文件所有的section', sections) options = conf.options('mysql') print('获取指定section下所有option', options) items = conf.items('mysql') print('获取指定section下所有的键值对', items) value = conf.get('mysql', 'host') print('获取指定的section下的option', type(value), value)

运行结果如下:

file_path : /Users/xxx/Desktop/xxx/xxx/xxx.ini
获取配置文件所有的section ['logging', 'mysql']
获取指定section下所有option ['host', 'port', 'user', 'password']
获取指定section下所有的键值对 [('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]
获取指定的section下的option <class 'str'> 127.0.0.1

综合使用方法:

import configparser """ 读取配置文件信息 """ class ConfigParser(): config_dic = {} @classmethod def get_config(cls, sector, item): value = None try: value = cls.config_dic[sector][item] except KeyError: cf = configparser.ConfigParser() cf.read('settings.ini', encoding='utf8') #注意setting.ini配置文件的路径 value = cf.get(sector, item) cls.config_dic = value finally: return value if __name__ == '__main__': con = ConfigParser() res = con.get_config('logging', 'level') print(res)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计694个文字,预计阅读时间需要3分钟。

如何使用Python configparser模块的常用方法进行配置文件解析?

%E2%80%9CConfigParser%E6%A8%A1%E5%9D%97%E5%9C%A8Python%E4%B8%AD%E7%94%A8%E6%9D%A5%E8%AF%BB%E5%8F%96%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E3%80%82%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E6%98%AF%E4%B8%80%E7%A7%8D%E6%A0%BC%E5%BC%8F%EF%BC%8C%E5%8F%AF%E4%BB%A5%E5%8C%85%E5%90%AB%E5%A4%9A%E4%B8%AA%E8%8A%82%E3%80%82%E6%AF%8F%E4%B8%AA%E8%8A%82%E5%8F%AF%E4%BB%A5%E5%8C%85%E5%90%AB%E5%A4%9A%E4%B8%AA%E5%8F%82%E6%95%B0%E3%80%82%E2%80%9D

ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用在程序员写死,可以使程序更灵活。

注意:在python 3 中ConfigParser模块名已更名为configparser

configparser函数常用方法:

读取配置文件:

read(filename) #读取配置文件,直接读取ini文件内容

sections() #获取ini文件内所有的section,以列表形式返回['logging', 'mysql']

如何使用Python configparser模块的常用方法进行配置文件解析?

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类型

举例如下:

配置文件ini如下:

[logging]
level = 20
path =
server =

[mysql]
host=127.0.0.1
port=3306
user=root
password=123456

注意,也可以使用:替换=

代码如下:

import configparser from until.file_system import get_init_path conf = configparser.ConfigParser() file_path = get_init_path() print('file_path :',file_path) conf.read(file_path) sections = conf.sections() print('获取配置文件所有的section', sections) options = conf.options('mysql') print('获取指定section下所有option', options) items = conf.items('mysql') print('获取指定section下所有的键值对', items) value = conf.get('mysql', 'host') print('获取指定的section下的option', type(value), value)

运行结果如下:

file_path : /Users/xxx/Desktop/xxx/xxx/xxx.ini
获取配置文件所有的section ['logging', 'mysql']
获取指定section下所有option ['host', 'port', 'user', 'password']
获取指定section下所有的键值对 [('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]
获取指定的section下的option <class 'str'> 127.0.0.1

综合使用方法:

import configparser """ 读取配置文件信息 """ class ConfigParser(): config_dic = {} @classmethod def get_config(cls, sector, item): value = None try: value = cls.config_dic[sector][item] except KeyError: cf = configparser.ConfigParser() cf.read('settings.ini', encoding='utf8') #注意setting.ini配置文件的路径 value = cf.get(sector, item) cls.config_dic = value finally: return value if __name__ == '__main__': con = ConfigParser() res = con.get_config('logging', 'level') print(res)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。