常用模块 - configparse模块
2024-09-03 22:14:58
一、简介
configparser模块在Python中是用来读取配置文件的,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节点(section),每个节可以有多个参数(键=值)。
二、生成配置文件
#! /usr/bin/env python3
# -*- coding:utf-8 -*- # Author : mayi
# Blog : http://www.cnblogs.com/mayi0312/
# Date : 2019/4/3
# Name : test01
# Software : PyCharm
# Note : 用于测试configparser模块的功能
# 导入模块
import configparser config = configparser.ConfigParser()
"""生成configparser配置文件 ,字典的形式"""
"""第一种写法"""
config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': ''}
"""第二种写法"""
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
"""第三种写法"""
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here config['DEFAULT']['ForwardX11'] = 'yes'
"""写入后缀为.ini的文件"""
with open('example.ini', 'w') as configfile:
config.write(configfile)
运行后,文件“example.ini”中的结果:
[DEFAULT]
compression = yes
compressionlevel = 9
serveraliveinterval = 45
forwardx11 = yes [bitbucket.org]
user = hg [topsecret.server.com]
host port = 50022
forwardx11 = no
三、解析配置文件
读取configparser配置文件的实例
#! /usr/bin/env python3
# -*- coding:utf-8 -*- # Author : mayi
# Blog : http://www.cnblogs.com/mayi0312/
# Date : 2019/4/3
# Name : test01
# Software : PyCharm
# Note : 用于测试configparser模块的功能
# 导入模块
import configparser config = configparser.ConfigParser()
# 读取配置文件
config.read("example.ini") print("所有节点==>", config.sections()) print("包含实例范围默认值的词典==>", config.defaults()) for item in config["DEFAULT"]:
print("循环节点topsecret.server.com下所有option==>", item) print("bitbucket.org节点下所有option的key,包括默认option==>", config.options("bitbucket.org")) print("输出元组,包括option的key和value", config.items('bitbucket.org')) print("bitbucket.org下user的值==>", config["bitbucket.org"]["user"]) # 方式一 topsecret = config['bitbucket.org']
print("bitbucket.org下user的值==>", topsecret["user"]) # 方式二 print("判断bitbucket.org节点是否存在==>", 'bitbucket.org' in config) print("获取bitbucket.org下user的值==>", config.get("bitbucket.org","user")) print("获取option值为数字的:host port=", config.getint("topsecret.server.com","host port"))
运行结果:
所有节点==> ['bitbucket.org', 'topsecret.server.com']
包含实例范围默认值的词典==> OrderedDict([('compression', 'yes'), ('compressionlevel', ''), ('serveraliveinterval', ''), ('forwardx11', 'yes')])
循环节点topsecret.server.com下所有option==> compression
循环节点topsecret.server.com下所有option==> compressionlevel
循环节点topsecret.server.com下所有option==> serveraliveinterval
循环节点topsecret.server.com下所有option==> forwardx11
bitbucket.org节点下所有option的key,包括默认option==> ['user', 'compression', 'compressionlevel', 'serveraliveinterval', 'forwardx11']
输出元组,包括option的key和value [('compression', 'yes'), ('compressionlevel', ''), ('serveraliveinterval', ''), ('forwardx11', 'yes'), ('user', 'hg')]
bitbucket.org下user的值==> hg
bitbucket.org下user的值==> hg
判断bitbucket.org节点是否存在==> True
获取bitbucket.org下user的值==> hg
获取option值为数字的:host port= 50022
删除配置文件section和option的实例(默认分组有参数时无法删除,但可以先删除下面的option,再删分组)
#! /usr/bin/env python3
# -*- coding:utf-8 -*- # Author : mayi
# Blog : http://www.cnblogs.com/mayi0312/
# Date : 2019/4/3
# Name : test01
# Software : PyCharm
# Note : 用于测试configparser模块的功能
# 导入模块
import configparser config = configparser.ConfigParser()
# 读取配置文件
config.read("example.ini") config.remove_section("bitbucket.org")
"""删除分组"""
config.remove_option("topsecret.server.com", "host port")
"""删除某组下面的某个值"""
config.write(open('example.ini', "w"))
运行后,文件“example.ini”中的结果:
[DEFAULT]
compression = yes
compressionlevel = 9
serveraliveinterval = 45
forwardx11 = yes [topsecret.server.com]
forwardx11 = no
修改配置文件
#! /usr/bin/env python3
# -*- coding:utf-8 -*- # Author : mayi
# Blog : http://www.cnblogs.com/mayi0312/
# Date : 2019/4/3
# Name : test01
# Software : PyCharm
# Note : 用于测试configparser模块的功能
# 导入模块
import configparser config = configparser.ConfigParser()
# 读取配置文件
config.read("example.ini") config.add_section("new_section")
"""新增分组"""
config.set("DEFAULT", "compressionlevel", "")
"""设置DEFAULT分组下compressionlevel的值为110"""
config.write(open('example.ini', "w"))
运行后,文件“example.ini”中的结果:
[DEFAULT]
compression = yes
compressionlevel = 110
serveraliveinterval = 45
forwardx11 = yes [topsecret.server.com]
forwardx11 = no [new_section]
最新文章
- 基于Ubuntu Hadoop的群集搭建Hive
- 构建ceph deb 安装包
- QT5学习过程的小问题集锦
- Android init.rc执行顺序
- Rhel6-cacti+nagios+ganglia(apache)配置文档
- SU Demos-02Filtering-07Sumedian
- Mixing Delphi and C++(相互调用)
- python操作mongodb之四cp数据库
- UVaLive 6609 Meeting Room Arrangement (贪心,区间不相交)
- iOS应用中URL地址如何重定向-b
- 自己动手画一个HTML5的按钮
- android edittext 点击回车会响应两次的解决方案
- 重新启动linux上的tomcat
- HDOJ 1217 Floyed Template
- Android bluetooth介绍(四): a2dp connect流程分析
- jvm性能优化及内存分区
- Number和toString中的坑
- 网络学习day04_VLSM、子网划分
- Swift 编程语言【转载+整理】
- Java最快的maven仓库地址,国内Maven地址,超快的Maven地址
热门文章
- [linux]查看进程占用内存
- Android Studio 3.4 修改 .android 和.gradle缺省目录-windows7x64专业版环境。
- pg中与超时设置有关的参数
- 如何更换linux shell中所显示目录的颜色?
- npm 更换阿里云镜像
- docker 启动mysql 闪退 无法启动问题
- 关于Flink slot 和kafka topic 分区关系的说明
- Xcode中opengl的配置
- liunx 定时任务工具 cron crontab
- maven工程仿springboot手写代码区分开发测试生产