打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python3 中 configparser 模块解析配置的用法详解

configparser 简介

configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近。Python2.x 中名为 ConfigParser,3.x 已更名小写,并加入了一些新功能。
配置文件的格式如下:

[DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLevel = 9ForwardX11 = yes[bitbucket.org]User = Tom[topsecret.com]Port: 50022ForwardX11: no
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

“[ ]”包含的为 section,section 下面为类似于 key - value 的配置内容;
configparser 默认支持 ‘=’ ‘:’ 两种分隔。


configparser 常用方法

初始化实例

使用 configparser 首先需要初始化实例,并读取配置文件:

>>> import configparser>>> config = configparser.ConfigParser()    # 注意大小写>>> config.read("config.ini")   # 配置文件的路径["config.ini"]
  • 1
  • 2
  • 3
  • 4

或者可以直接读字典

>>> parser = configparser.ConfigParser()>>> parser.read_dict({'section1': {'key1': 'value1',...                                'key2': 'value2',...                                'key3': 'value3'},...                   'section2': {'keyA': 'valueA',...                                'keyB': 'valueB',...                                'keyC': 'valueC'},...                   'section3': {'foo': 'x',...                                'bar': 'y',...                                'baz': 'z'}... })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

获取所有 sections

>>> config.sections()['bitbucket.org', 'topsecret.com']    # 注意会过滤掉[DEFAULT]
  • 1
  • 2

获取指定 section 的 keys & values

>>> config.items('topsecret.com')>>>> [('port', '50022'), ('forwardx11', 'no')]    # 注意items()返回的字符串会全变成小写
  • 1
  • 2

获取指定 section 的 keys

>>> config.options('topsecret.com')['Port', 'ForwardX11']
  • 1
  • 2
>>> for option in config['topsecret.com']:...     print(option)PortForwardX11
  • 1
  • 2
  • 3
  • 4

获取指定 key 的 value

>>> config['bitbucket.org']['User']'Tom'
  • 1
  • 2
>>> config.get('bitbucket.org', 'User')'Tom'>>> config.getint('topsecret.com', 'Port')50022
  • 1
  • 2
  • 3
  • 4

检查

>>> 'DEFAULT' in configTrue>>> 'test' in config['section_test']False>>> 'Tom' in config['bitbucket.org']['User']True
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
>>> config.has_section('bitbucket.org')True>>> config.has_option('section_test', 'test')False
  • 1
  • 2
  • 3
  • 4

添加

>>> config.add_section('Section_1')>>> config.set('Section_1', 'key_1', 'value_1')    # 注意键值是用set()方法>>> config.write(open('config.ini', 'w'))    # 一定要写入才生效
  • 1
  • 2
  • 3

删除

>>> config.remove_option('Section_1', 'key_1')True>>> config.remove_section('Section_1')True>>> config.clear()  # 清空除[DEFAULT]之外所有内容>>> config.write(open('config.ini', 'w'))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

关于 [DEFAULT]

[DEFAULT] 一般包含 ini 格式配置文件的默认项,所以 configparser 部分方法会自动跳过这个 section 。
前面已经提到 sections() 是获取不到的,还有删除方法对 [DEFAULT] 也无效:

>>> config.remove_section('DEFAULT')False>>> config.clear()>>> 'DEFAULT' in configTrue>>> 'ForwardX11' in config['DEFAULT']True>>> config.sections()[]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

但指定删除和修改 [DEFAULT] 里的 keys & values 是可以的:

>>> config.remove_option('DEFAULT', 'ForwardX11')True>>> config.set('DEFAULT', 'ForwardX11','no')>>> config['DEFAULT']['ForwardX11']'no'
  • 1
  • 2
  • 3
  • 4
  • 5

还有个特殊的是,has_section() 也无效,可以和 in 区别使用

>>> config.has_section('DEFAULT')False>>> 'DEFAULT' in configTrue
  • 1
  • 2
  • 3
  • 4


更多用法请看官方文档:https://docs.python.org/3.6/library/configparser.html

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python 操作ini 配置文件大全
Python3中的configparser模块
Python配置文件解析configparser
Python 操作 ini文件
python读写ini格式的配置文件
Python配置文件模块埋个坑,险些影响了版本上线!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服