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模块的更多相关文章

  1. python学习 day19 configparser模块 os模块 subprocess模块

    上周五回顾 logging 用于记录日志 四种核心角色: 生成器Logger 过滤器Filter 处理器Handler 格式化处理器 Formatter logging.info.debug 使用默认 ...

  2. Python学习 Part4:模块

    Python学习 Part4:模块 1. 模块是将定义保存在一个文件中的方法,然后在脚本中或解释器的交互实例中使用.模块中的定义可以被导入到其他模块或者main模块. 模块就是一个包含Python定义 ...

  3. python:利用configparser模块读写配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  4. python学习之argparse模块

    python学习之argparse模块 一.简介: argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块.argparse模块的作用是用于解析命令行 ...

  5. Python 标准库 ConfigParser 模块 的使用

    Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...

  6. Python学习day19-常用模块之re模块

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  7. Python学习day18-常用模块之NumPy

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  8. Python学习笔记-常用模块

    1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...

  9. Python自动化测试 (二) ConfigParser模块读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section,    section 下有op ...

随机推荐

  1. Android获取本地相册图片、拍照获取图片

    需求:从本地相册找图片,或通过调用系统相机拍照得到图片. 容易出错的地方: 1,当我们指定了照片的uri路径,我们就不能通过data.getData();来获取uri,而应该直接拿到uri(用全局变量 ...

  2. Android利用已有控件实现自定义控件

    Android控件的基本介绍及使用自定义控件的意义         Android 本身提供了很多控件,自定义控件在android中被广泛运用,自定义控件给了我们很大的方便.比如说,一个视图为imag ...

  3. (转)@Autowire注解与自动装配

    http://blog.csdn.net/yerenyuan_pku/article/details/52860713 前面我们已经学会使用@Resource注解注入属性,并且我们还编码剖析了@Res ...

  4. Dreamoon and MRT(二元枚举)

    题目 数轴上有M个点a1.a2....am,另有一个数列p1.p2....pn,(1 ≤ pii ≤ M). 给定d1.d2....dn,对所有的 i (1 ≤ i ≤ n),已知 |api+1 - ...

  5. docker 镜像仓库的安装与使用

    安装Docker Compose 解决依赖 [root@service-1 ~]# curl -L "https://github.com/docker/compose/releases/d ...

  6. 树状数组 || POJ 2352 Stars

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  7. 1-jdk的安装与配置

    1- Jvm.jdk.jre之间的关系 JVM:Java虚拟机,保证java程序跨平台.(Java Virtual Machine) JRE: Java运行环境,包含JVM和核心类库.如果只是想运行j ...

  8. FileZilla Server安装配置教程

    1. FileZilla官网下载FileZilla Server服务器,目前最新版本为0.9.53. 2. 安装FileZilla服务器.除以下声明的地方外,其它均采用默认模式,如安装路径等. 2.1 ...

  9. Spring框架针对dao层的jdbcTemplate操作crud之update修改数据库操作

    使用jdbcTemplate 原理是把加载驱动Class.forName("com.mysql.jdbc.Driver"); 和连接数据库Connection conn=Drive ...

  10. Sqlserver查询结果集插入新表

    数据库“Test” 数据库“Test2” 表 “fromTable” 表 “toTable” 表 “newTable” 字段 “name”,“age”,“gender” 原因:公司有2个数据库,一个是 ...