该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。

创建文件

import configparser

config = configparser.ConfigParser()

config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9',
'ForwardX11':'yes'
} #DEFAULT是默认分组,名称必须是DEFAULT config['bitbucket.org'] = {'User':'hg'} # 每一个config[]是一个组 config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'} with open('example.ini', 'w') as configfile: config.write(configfile)

执行结果-'example.ini'文件内容

[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [bitbucket.org]
user = hg [topsecret.server.com]
host port = 50022
forwardx11 = no

文件内容

查找文件
向字典一样操作

import configparser
config = configparser.ConfigParser() # 需要先创建对象
config.read('example.ini') # 给对象绑定配置文件
print(config.sections()) # ['bitbucket.org', 'topsecret.server.com'] 返回配置文件中节序列-组名(不包含默认分组) print('bitbucket.org' in config) # False 判断某个分组是不是在文件里
print(config['bitbucket.org']["user"]) # hg 查看某组的某项
print(config['bitbucket.org']["compressionlevel"]) # 9 默认组的内容被其他组共享
print(config['topsecret.server.com']['ForwardX11']) # no 当子组中存在与默认组相同的项时,子组查询会使用子组内容
print(config['bitbucket.org']) # <Section: bitbucket.org>
for key in config['bitbucket.org']: # 注意,有default组会打印default组的键,子组优先
print(key)
print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键,返回一个列表
print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对,键值对以元组形式返回,存放在列表里
print(config.get('bitbucket.org','compression')) # yes get方法Section下的key对应的value

增删改操作

import configparser
config = configparser.ConfigParser() # 需要先创建对象
config.read('example.ini') # 给对象绑定配置文件 # config.add_section('yuan') # 增加一个组
# config.remove_section('bitbucket.org') # 删除一个组
# config.remove_option('topsecret.server.com',"forwardx11") # 删除一个组中的一项
# config.set('topsecret.server.com','compression','yes') #给一个组中增加或更改一项, config.write(open('example.ini', "w")) # 把修改后的内容写会文件才会生效

  

configparser模块--配置文件的更多相关文章

  1. python ConfigParser模块 配置文件解析

    ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] da ...

  2. hashlib,suprocess,configparser模块

    十 hashlib模块 1.什么叫hash:hash是一种算法,该算法接受传入的内容,经过运算得到一串hash值 2.hash值的特点是: 2.1 只要传入的内容一样,得到的hash值必然一样==== ...

  3. python之shelve、xml、configparser模块

    一.shelve模块 shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型 import shelve ...

  4. 用ConfigParser模块读写配置文件——Python

    对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...

  5. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

  6. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

  7. Python模块之ConfigParser - 读写配置文件

    Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...

  8. python:实例化configparser模块读写配置文件

    之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...

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

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

随机推荐

  1. Linux记录-salt命令

    salt '*id*'  test.ping salt -N  组名  cmd.run '' salt -G "ipv4:0.0.0.0"  cmd.run '' salt '*i ...

  2. Linux记录-文件格式

    yum -y install dos2unix dos2unix filename

  3. bzoj千题计划318:bzoj1396: 识别子串(后缀自动机 + 线段树)

    https://www.lydsy.com/JudgeOnline/problem.php?id=1396 后缀自动机的parent树上,如果不是叶子节点,那么至少有两个子节点 而一个状态所代表子串的 ...

  4. Ajax和Json实现自动补全

    1.index.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...

  5. 判断质数(Java)

    package day01; //输出1-100中质数,并且每十个换行 public class PrimeNum { public static void main(String[] args) { ...

  6. Filter 起航 编程式配置 压缩响应 日志过滤器

    [编程式配置]可以用web.xml配置替换 @WebListenerpublic class FilterListenerConfigurator implements ServletContextL ...

  7. [C++]数组处理相关函数(memcpy/memset等)

    头文件:string.h或者memory.h [1]void *memcpy(void *dest, const void *src, size_t n);//数组元素拷贝 功能:从源src所指的内存 ...

  8. ASLP Kaldi

    ASLP(Audio, Speech and Language Processing Group,音频.语音和语言处理组)位于西北工业大学,隶属于陕西省语音和图像信息处理重点实验室(SAIIP). A ...

  9. A Bayesian Approach to Deep Neural Network Adaptation with Applications to Robust Automatic Speech Recognition

    基于贝叶斯的深度神经网络自适应及其在鲁棒自动语音识别中的应用     直接贝叶斯DNN自适应 使用高斯先验对DNN进行MAP自适应 为何贝叶斯在模型自适应中很有用? 因为自适应问题可以视为后验估计问题 ...

  10. 【blog】SpringBoot普通类中如何获取其他bean例如Service、Dao

    自己写工具类 工具类 import org.springframework.beans.BeansException; import org.springframework.context.Appli ...