python学习之 - configparser模块
configparser模块
功能:用于生成和修改常见配置文件。
基本常用方法如下:
read(filename):直接读取配置文件
write(filename):将修改后的配置文件写入文件中。
defaults():返回全部示例中所有defaults
sections():得到所有的section,并以列表的形式返回
items(section):得到该section的所有键值对
has_section(section):检查是否有section,有返回True,无返回False
has_option(section,option):检查section下是否有指定的option,有返回True,无返回False
getint(section,option):得到section中option的值,返回为int类型
get(section,option):得到section中option的值,返回为string类型
getboolean(section,option):得到section中option的值,返回为boolean类型
getfloat(section,option):得到section中option的值,返回为float类型
add_section(section):添加一个新的section
remove_section(section):删除某个section
options(section):得到该section的所有option
set(section,option,value):对section中的option进行设置,需要调用write将内容写入配置文件
remove_option(section,option):删除某个section下的option
能处理的配置文件格式如下:
[mysql] #节点名
键 = 值
举例1:自动生成一个配置文件
import configparser
# 自动生成配置文件
cfg = configparser.ConfigParser()
# 创建一个全局配置
cfg['DEFAULT'] = {
'ServerAliveInterval':'',
'Compression':'yes',
'CompressionLevel':''
}
#创建局部配置
cfg['bitbucket.org'] = {}
cfg['bitbucket.org']['User'] = 'hg'
cfg['topsecret.server.com'] = {}
topsecret = cfg['topsecret.server.com']
topsecret['host port'] = ''
topsecret['ForwardX11'] = 'no'
cfg['DEFAULT']['ForwardX11'] = 'yes'
# 配置完成,重写文件
with open('example.ini','w') as f:
cfg.write(f)
###以下进行配置文件的 增/删/改/查 功能实例
先导入模块读取配置文件
import configparser
cfg = configparser.ConfigParser()
# 读取配置文件
cfg.read('example.ini',encoding='utf-8')
# 提取所有第一层节点信息以列表打印
info = cfg.sections()
print(info)
-- 增 操作
# 判断节点是否存在
sec = cfg.has_section('name')
# 判断节点下的text键是否存在
sec = cfg.has_option('name','text')
# 增加节点操作
sec = cfg.add_section('name')
-- 修改/增加 为节点增加或修改一行数据 操作
cfg.set('name','text','jeck')
cfg.write(open('examp2.ini','w'))
-- 删 操作
# 删除bitbucket.org节点所有数据
sec = cfg.remove_section('bitbucket.org')
cfg.write(open('examp1.ini','w'))
# 删除节点下一行数据
sec = cfg.remove_option('topsecret.server.com','host port')
cfg.write(open('examp2.ini','w'))
-- 查 操作
# 单独打印default信息
print(cfg.defaults())
# 以元组列表形式打印指定节点下所有信息(会连带默认打印default信息)
print(cfg.items('bitbucket.org'))
# 查找打印匹配信息
print(cfg['bitbucket.org']['user']) 类似于 print(cfg.get('bitbucket.org','user'))
print(cfg['DEFAULT']['forwardx11'])
section_name = cfg.sections()[1]
print(cfg[section_name]['host port'])
# 循环打印指定节点下所有信息
for k,v in cfg[section_name].items():
print(k,v) # 打印指定节点和默认节点所有的Key
print(cfg.options('topsecret.server.com'))
for k,v in cfg.items('topsecret.server.com'):
print(k,v)
python学习之 - configparser模块的更多相关文章
- python学习 day19 configparser模块 os模块 subprocess模块
		上周五回顾 logging 用于记录日志 四种核心角色: 生成器Logger 过滤器Filter 处理器Handler 格式化处理器 Formatter logging.info.debug 使用默认 ... 
- Python学习 Part4:模块
		Python学习 Part4:模块 1. 模块是将定义保存在一个文件中的方法,然后在脚本中或解释器的交互实例中使用.模块中的定义可以被导入到其他模块或者main模块. 模块就是一个包含Python定义 ... 
- python:利用configparser模块读写配置文件
		在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ... 
- python学习之argparse模块
		python学习之argparse模块 一.简介: argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块.argparse模块的作用是用于解析命令行 ... 
- Python 标准库 ConfigParser 模块 的使用
		Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ... 
- Python学习day19-常用模块之re模块
		figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ... 
- Python学习day18-常用模块之NumPy
		figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ... 
- Python学习笔记-常用模块
		1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ... 
- Python自动化测试 (二) ConfigParser模块读写配置文件
		ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section, section 下有op ... 
随机推荐
- BeanUtils.copyProperties(productInfo, productInfoVO);
			一:spring的工具类方法:BeanUtils.copyProperties(orderMasterDTO, orderMasterDO); 作用:将orderMasterDTO对象中的属性值,赋值 ... 
- SQLite-And和OR运算符
			SQLite - AND 和 OR 运算符 SQLite AND . OR运算符用于编译多个条件缩小在一个SQLite声明中选定的数据.这两个操作符被称为连接的操作符. 这些操作符与不同操作提供了一种 ... 
- 升级或者重装Discuz! 版本后 QQ互联英文乱码显示的正确解决方法
			升级Discuz! X3版本QQ互联英文乱码!connect_viewthread_share_to_qq! 目前Discuz!论坛上 最简单的解决方法: 第一步:后台----->站长---- ... 
- uva1619 Feel Good
			单调队列,滑动窗口 int t=0; while(scanf("%d",&n)==1){ if(t) printf("\n"); //有点方便 单调队列 ... 
- Vue 2.0 右键菜单组件 Vue Context Menu
			Vue 2.0 右键菜单组件 Vue Context Menu https://juejin.im/entry/5976d14751882507db6e839c 
- selective_search_rcnn.m中代码
			im = imresize(im, [NaN im_width]):把图像转换为宽度为im_width,自动计算列数 
- mybatis 实现批量更新
			更新单条记录 1 UPDATE course SET name = 'course1' WHERE id = 'id1'; 更新多条记录的同一个字段为同一个值 1 UPDATE course SET ... 
- ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath
			问题: ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the ... 
- ajax 请求json数据中json对象的构造获取问题
			前端的界面中,我想通过ajax来调用写好的json数据,并调用add(data)方法进行解析,请求如下: json数据如下: { “type”:"qqq", "lat&q ... 
- jquery-closest
			1.closest() 本例演示如何通过 closest() 完成事件委托.当被最接近的列表元素或其子后代元素被点击时,会切换黄色背景: $( document ).bind("click& ... 
