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. CCF|中间数|Java

    import java.util.*; public class tyt { public static void main(String[] args) { Scanner in = new Sca ...

  2. ios-获取系统相簿里边的所有照片

    #import<AssetsLibrary/AssetsLibrary.h> -(void)getImgs{ dispatch_async(dispatch_get_main_queue( ...

  3. android应用流量信息提取

    Linux 系统下所有的信息都是以文件的形式存在的,所以应用程序的流量信息也会被保存在操作系统的文件中.Android 2.2 版本以前的系统的流量信息都存放在 proc/net/dev(或者 pro ...

  4. Android(java)学习笔记175:Android进程间通讯(IPC)之AIDL

    一.IPC inter process communication  进程间通讯 二.AIDL android  interface  defination  language  安卓接口定义语言 满 ...

  5. 什么是cookie(前段时间看到别人简历上把cookie和localStorage混淆了所以专门又去了解了下)

    在前端面试中,有一个必问的问题:请你谈谈cookie和localStorage有什么区别啊? localStorage是H5中的一种浏览器本地存储方式,而实际上,cookie本身并不是用来做服务器存储 ...

  6. Window服务程序(windows service application)如何调试

    服务程序不能通过常规的按F5或F11的方式来进行调试和运行,也无法立即运行一个服务或逐步调试它的代码. 因此,你必须安装并启动你的服务,然后附属(attach)一个Debugger到这个服务的进程上.

  7. postman使用--批量执行测试用例和数据驱动

    批量执行 在我们测试接口的时候,有时候希望执行所有的测试用例,前面讲的都是测试单个的接口,postman提供了我们批量执行接口的功能 点击Runner 然后我们点击run 执行完会统计出我们的结果,失 ...

  8. 配置maven报错 the java_home environment variable is not defined correctly ......

    the java_home environment variable is not defined correctly This environment variable is needed to r ...

  9. C指针计算字符串长度

    #include <stdio.h> int stringLength (const char *string) { const char *cptr = string; while ( ...

  10. SpringBoot的线程调度

    Spring Boot默认提供了一个ThreadPoolTaskExecutor作为线程调度器,只需要在配置类中使用注解EnableAsync即可开启异步线程调度.在实际要执行的Bean中使用@Asy ...