常用模块 - configparse模块
一、简介
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]
常用模块 - configparse模块的更多相关文章
- python模块: hashlib模块, configparse模块, logging模块,collections模块
一. hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用 ...
- Python模块-configparse模块
configparse模块用来解析配置文件 配置文件 [DEFAULT] port = 3306 socket = /tmp/mysql.sock [mysqldump] max_allowed_pa ...
- 第十一节:configParse模块
作用:配置文件解析模块,用来增删改查配置文件内容,不区分大小写 配置文件案例: tets.ini [模块] key=value import configparser config = configp ...
- Python常用内建模块
Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...
- 转:Yii实战中8个必备常用的扩展,模块和widget
转载自:http://www.yiiframework.com/wiki/180/yii8/ 在经过畅K网的实战后,总结一下在Yii的项目中会经常用到的组件和一些基本的使用方法,分享给大家,同时也给自 ...
- python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块
正则表达式 语法: mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...
- python configparse模块&xml模块
configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...
- Python之常用模块--collections模块
认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...
- 【转】Python3 configparse模块(配置)
[转]Python3 configparse模块(配置) ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(s ...
随机推荐
- AWS研究热点:BMXNet – 基于MXNet的开源二进神经网络实现
http://www.atyun.com/9625.html 最近提出的二进神经网络(BNN)可以通过应用逐位运算替代标准算术运算来大大减少存储器大小和存取率.通过显着提高运行时的效率并降低能耗,让最 ...
- odoo开发笔记 -- 提高访问安全性的一种方式
场景描述: 最近在做项目的过程中,需要需要将odoo应用集成到其他系统中, 在对方的系统中点击我们的应用,打开对应系统,但是界面不做跳转,在当前页面打开,并且浏览器地址栏只显示IP+应用名,不让显示o ...
- 运维笔记--SqlServer相关版本&下载&安装&配置远程连接
下载地址:SqlServer2008为例 SqlServer2008:https://www.microsoft.com/en-us/download/details.aspx?id=1695 Sql ...
- WebGL学习笔记(十二):加载模型文件
目前为止,我们用到的模型顶点uv信息等,都是直接定义在代码中的,实际使用中,这些数据应该是由3D编辑器编辑好后按照一定的格式存储在文件中的,我们需要从文件中提取出对应的数据之后,组合成我们可以使用的信 ...
- winform调用webservice假死怎么解决
主线程调用外部web service,没有返回时,主线程阻塞了,界面肯定假死耗时操作都是要在工作线程里面执行的.一般情况下winform调用webservice时步骤1添加服务引用---高级----添 ...
- pytorch中使用cuda扩展
以下面这个例子作为教程,实现功能是element-wise add: (pytorch中想调用cuda模块,还是用另外使用C编写接口脚本) 第一步:cuda编程的源文件和头文件 // mathutil ...
- SDKMAN一个基于命令行界面的SDK用户环境管理程序
1.背景 使用过Python开发的朋友,应该了解到Python2和Python3语法的差异,有时候从网上下载了基于不同解释器的代码,要来回切换版本, 使用起来不是很方便,有时候甚至很麻烦.于是有人发明 ...
- EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器版本打包方法介绍
EasyDSS流媒体解决方案是一套集流媒体点播.转码.管理.直播.录像.检索.时移回看于一体的一套完整的商用流媒体解决方案.EasyDSS软件以压缩包的形式提供给客户使用,同时支持Linux和 Win ...
- Spring Boot程序正确停止的姿势
Spring Boot提供了2种优雅关闭进程的方式: 基于管理端口关闭进程 基于系统服务方式关闭进程 基于管理端口关闭进程 基于管理端口方式实现进程关闭实际上是模块spring-boot-actuat ...
- 【转】什么是5G?居然有人用漫画把它讲得如此接地气!
最近一系列层出不穷的新闻,似乎都离不开一个关键词——5G.在各大报道中,都提到5G网络是移动无线技术的下一个重要发展. 任正非之前也在采访中说过: “5G,别人两三年也不会追上我们的.” “5G并不是 ...