Python configparser模块应用过程解析
作者:蓝 寅 时间:2022-08-03 19:56:11
一、configparser模块是什么
可以用来操作后缀为 .ini 的配置文件;
python标准库(就是python自带的意思,无需安装)
二、configparser模块基本使用
2.1 读取 ini 配置文件
#存在 config.ini 配置文件,内容如下:
[DEFAULT]
excel_path = ../test_cases/case_data.xlsx
log_path = ../logs/test.log
log_level = 1
[email]
user_name = 32@qq.com
password = 123456
使用configparser模块读取配置文件
import configparser
#创建配置文件对象
conf = configparser.ConfigParser()
#读取配置文件
conf.read('config.ini', encoding="utf-8")
#列表方式返回配置文件所有的section
print( conf.sections() ) #结果:['default', 'email']
#列表方式返回配置文件email 这个section下的所有键名称
print( conf.options('email') ) #结果:['user_name', 'password']
#以[(),()]格式返回 email 这个section下的所有键值对
print( conf.items('email') ) #结果:[('user_name', '32@qq.com'), ('password', '123456')]
#使用get方法获取配置文件具体的值,get方法:参数1-->section(节) 参数2-->key(键名)
value = conf.get('default', 'excel_path')
print(value)
2.2 写入 ini 配置文件(字典形式)
import configparser
#创建配置文件对象
conf = configparser.ConfigParser()
#'DEFAULT'为section的名称,值中的字典为section下的键值对
conf["DEFAULT"] = {'excel_path' : '../test_cases/case_data.xlsx' , 'log_path' : '../logs/test.log'}
conf["email"] = {'user_name':'32@qq.com','password':'123456'}
#把设置的conf对象内容写入config.ini文件
with open('config.ini', 'w') as configfile:
conf.write(configfile)
2.3 写入 ini 配置文件(方法形式)
import configparser
#创建配置文件对象
conf = configparser.ConfigParser()
#读取配置文件
conf.read('config.ini', encoding="utf-8")
#在conf对象中新增section
conf.add_section('webserver')
#在section对象中新增键值对
conf.set('webserver','ip','127.0.0.1')
conf.set('webserver','port','80')
#修改'DEFAULT'中键为'log_path'的值,如没有该键,则新建
conf.set('DEFAULT','log_path','test.log')
#删除指定section
conf.remove_section('email')
#删除指定键值对
conf.remove_option('DEFAULT','excel_path')
#写入config.ini文件
with open('config.ini', 'w') as f:
conf.write(f)
上述3个例子基本阐述了configparser模块的核心功能项;
例1中,encoding="utf-8"为了放置读取的适合中文乱码;
例2你可以理解为在字典中新增数据,键:配置文件的section,字符串格式;值:section的键值对,字典格式;
例3中在使用add_section方法时,如果配置文件存在section,则会报错;而set方法在使用时,有则修改,无则新建。
来源:https://www.cnblogs.com/dream66/p/12605038.html
标签:Python,configparser,模块
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python设计模式创建型原型模式
2023-07-21 21:19:25
![](https://img.aspxhome.com/file/2023/4/59124_0s.png)
php floor()函数案例详解
2023-06-14 16:13:03
php利用ob_start()清除输出和选择性输出的方法
2023-11-18 08:54:35
asp下几种常用排序算法
2011-04-18 10:33:00
Python 专题五 列表基础知识(二维list排序、获取下标和处理txt文本实例)
2023-09-03 10:22:39
![](https://img.aspxhome.com/file/2023/4/61234_0s.png)
Python中的TCP socket写法示例
2023-06-25 00:21:05
![](https://img.aspxhome.com/file/2023/7/60707_0s.jpg)
Oracle数据库集复制方法浅议
2010-07-21 12:50:00
X/HTML5 v.s. XHTML2(I)
2008-06-17 18:00:00
ASP正则表达式验证域名是否合法
2010-01-02 20:44:00
在ASP中使用SQL语句之9:表单操作
2007-08-11 13:18:00
list视图方式设计浅析
2008-12-21 16:04:00
![](https://img.aspxhome.com/file/UploadPic/200812/21/01-28s.jpg)
MySQL5创建存储过程实例
2010-06-13 12:49:00
在ASP应用程序中加入智能搜索
2007-09-18 13:15:00
php投票系统之增加与删除投票(管理员篇)
2023-10-14 09:44:53
![](https://img.aspxhome.com/file/2023/0/55710_0s.png)
MySQL数据库常见的出错代码及出错信息
2008-05-27 12:29:00
网页语言编码及asp乱码问题解决方案
2008-01-31 13:21:00
django admin后台添加导出excel功能示例代码
2023-10-19 10:43:48
![](https://img.aspxhome.com/file/2023/9/62309_0s.png)
asp去除html的函数代码分析附实例说明
2023-07-02 20:09:18
微信小程序日期选择器实例代码
2023-08-19 00:22:57
![](https://img.aspxhome.com/file/2023/7/56087_0s.png)
提高asp程序访问速度的方法
2008-10-23 16:37:00