Python3中configparser模块读写ini文件并解析配置的用法详解
作者:geerniya 时间:2022-11-02 12:11:14
Python3中configparser模块简介
configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近。Python2.x 中名为 ConfigParser,3.x 已更名小写,并加入了一些新功能。
配置文件的格式如下:
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[bitbucket.org]
User = Tom
[topsecret.com]
Port: 50022
ForwardX11: no
“[ ]”包含的为 section,section 下面为类似于 key - value 的配置内容;
configparser 默认支持 ‘=' ‘:' 两种分隔。
configparser 常用方法
初始化实例
使用 configparser 首先需要初始化实例,并读取配置文件:
>>> import configparser
>>> config = configparser.ConfigParser() # 注意大小写
>>> config.read("config.ini") # 配置文件的路径
["config.ini"]
或者可以直接读字典
>>> 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'}
... })
获取所有 sections
>>> config.sections()
['bitbucket.org', 'topsecret.com'] # 注意会过滤掉[DEFAULT]
获取指定 section 的 keys & values
>>> config.items('topsecret.com')
>>>> [('port', '50022'), ('forwardx11', 'no')] # 注意items()返回的字符串会全变成小写
获取指定 section 的 keys
>>> config.options('topsecret.com')
['Port', 'ForwardX11']
>>> for option in config['topsecret.com']:
... print(option)
Port
ForwardX11
获取指定 key 的 value
>>> config['bitbucket.org']['User']
'Tom'
>>> config.get('bitbucket.org', 'User')
'Tom'
>>> config.getint('topsecret.com', 'Port')
50022
configparser模块检查
>>> 'DEFAULT' in config
True
>>> 'test' in config['section_test']
False
>>> 'Tom' in config['bitbucket.org']['User']
True
>>> config.has_section('bitbucket.org')
True
>>> config.has_option('section_test', 'test')
False
configparser模块添加
>>> config.add_section('Section_1')
>>> config.set('Section_1', 'key_1', 'value_1') # 注意键值是用set()方法
>>> config.write(open('config.ini', 'w')) # 一定要写入才生效
configparser模块删除
>>> config.remove_option('Section_1', 'key_1')
True
>>> config.remove_section('Section_1')
True
>>> config.clear() # 清空除[DEFAULT]之外所有内容
>>> config.write(open('config.ini', 'w'))
关于 [DEFAULT]
[DEFAULT] 一般包含 ini 格式配置文件的默认项,所以 configparser 部分方法会自动跳过这个 section 。
前面已经提到 sections() 是获取不到的,还有删除方法对 [DEFAULT] 也无效:
>>> config.remove_section('DEFAULT')
False
>>> config.clear()
>>> 'DEFAULT' in config
True
>>> 'ForwardX11' in config['DEFAULT']
True
>>> config.sections()
[]
但指定删除和修改 [DEFAULT] 里的 keys & values 是可以的:
>>> config.remove_option('DEFAULT', 'ForwardX11')
True
>>> config.set('DEFAULT', 'ForwardX11','no')
>>> config['DEFAULT']['ForwardX11']
'no'
还有个特殊的是,has_section() 也无效,可以和 in 区别使用
>>> config.has_section('DEFAULT')
False
>>> 'DEFAULT' in config
True
来源:https://blog.csdn.net/geerniya/article/details/80083152
标签:Python3,configparser
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Win7环境下搭建Go开发环境(基于VSCode编辑器)
2024-04-30 09:59:04
![](https://img.aspxhome.com/file/2023/0/130920_0s.png)
给自定义对象加上自定义事件的支持的教程
2023-10-14 20:49:00
python matplotlib绘图实现删除重复冗余图例的操作
2023-11-02 22:36:44
sql server几种Join的区别测试方法
2024-01-13 14:54:18
解决mysql输入密码闪退的问题
2024-01-21 02:29:14
![](https://img.aspxhome.com/file/2023/2/119762_0s.png)
Python中使用Tkinter模块创建GUI程序实例
2023-01-22 09:37:38
kettle 入门使用教程(最新版)
2023-04-12 21:28:12
![](https://img.aspxhome.com/file/2023/0/131800_0s.jpg)
使用Python3中的gettext模块翻译Python源码以支持多语言
2022-10-17 21:36:07
![](https://img.aspxhome.com/file/2023/6/82976_0s.jpg)
mysql 导入导出数据
2011-02-23 12:27:00
Python 命令行非阻塞输入的小例子
2023-12-09 19:48:29
Mysql的基础使用之MariaDB安装方法详解
2024-01-14 15:03:46
![](https://img.aspxhome.com/file/2023/2/99352_0s.jpg)
python下对hsv颜色空间进行量化操作
2022-11-22 01:56:12
![](https://img.aspxhome.com/file/2023/6/93966_0s.jpg)
解决python3 urllib 链接中有中文的问题
2022-10-19 08:43:51
一步步教你用Python实现2048小游戏
2023-02-15 20:51:29
![](https://img.aspxhome.com/file/2023/9/82809_0s.jpg)
MySQL 不等于的三种使用及区别
2024-01-28 20:03:56
![](https://img.aspxhome.com/file/2023/9/105219_0s.png)
Python学生成绩管理系统简洁版
2023-08-23 01:33:08
详尽解析javascript event对象
2008-06-08 12:25:00
字符编码详解及由来(UNICODE,UTF-8,GBK) 比较详细
2022-05-21 14:49:29
pytorch中使用LSTM详解
2021-01-08 04:27:10
![](https://img.aspxhome.com/file/2023/4/89394_0s.jpg)
Python实现的朴素贝叶斯算法经典示例【测试可用】
2021-09-10 19:15:25