该模块适用于配置文件的格式与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. golang命令行参数

    os.Args获取命令行参数 os.Args是一个srting的切片,用来存储所有的命令行参数 package main import ( "fmt" "os" ...

  2. hdu 2815 Mod Tree (exBSGS)

    http://acm.hdu.edu.cn/showproblem.php?pid=2815 //解 K^D ≡ N mod P #include<map> #include<cma ...

  3. Spring事物的属性

    链接:https://www.nowcoder.com/questionTerminal/1c65d30e47fb4f59a5e5af728218cac4?orderByHotValue=2& ...

  4. ArcGis 制图——地图图框整饰的插件式实现(一)C#

    如有插件定制需求或技术交流,欢迎联系QQ 975601416 写完了自己瞅了一眼都不想看,希望有需要的你能看懂. 先摆一张效果图: 下面进入主题,本篇先讲一下地图布局中的对象,正文中会对一些关键词用英 ...

  5. 我手机上常用的app和常访问的网站

    ====常用======Opera Mini browser 浏览器(版本26.0.2254.117241以上) 老版本7.7最最经典, 但该版本在新的安卓手机上总有部分区域显示空白. 现在的 Ope ...

  6. DotNetBar的一个MDIView不正常显示的问题

    现象,使用tabStrip MDIView后,子窗体会被遮挡一部分,两种解决办法 1.tabStrip的 MdiAutoHide=False 2.tabStrip 设置MultilineWithNav ...

  7. ButterKnife官方使用例子

    Introduction Annotate fields with @BindView and a view ID for Butter Knife to find and automatically ...

  8. 一个不错的Html5 DatePicker控件,主要支持手机端。

    原地址不知道在哪了,下载地址 QQ群:616945527 Html5控件wen文件夹中DatePicker.rar

  9. 20155324 2016-2017-2 《Java程序设计》第1周学习总结

    20155324 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 1.1.1 前世今生 Java最早是Sun公司绿色项目Green Project中撰写Sta ...

  10. 第25月第2天 Django-By-Example项目记录01

    1. export PATH="$PATH":/Applications/XAMPP/xamppfiles/bin/ sudo ln -s /Applications/XAMPP/ ...