configparser模块

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

创建文件

import configparser

config = configparser.ConfigParser()

config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'ForwardX11':'yes'
} config['bitbucket.org'] = {'User':'hg'} config['topsecret.server.com'] = {'Host Port':'','ForwardX11':'no'} with open('example.ini', 'w') as configfile: config.write(configfile)

文件内容

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

查找文件内容

方法 描述
defaults() 返回包含实例范围默认值的字典。
sections() 返回可用的section的列表;默认section不包括在列表中
has_section(section) 指示指定的section是否出现在配置中。默认的section未被确认
options(section) 返回指定section中可用的选项列表。
has_option(section, option) 如果给定的section存在,并且包含给定的选项,则返回True;否则返回False
get(section, option) 为指定的section获取一个选项值。
getint(section, option) 它将指定section中的选项强制转换为整数
getfloat(section, option) 它将指定section中的选项强制转换为浮点型
getboolean(section, option) 强制转换为布尔型,”1”, “yes”, “true”, and “on”, 转换为True,”0”, “no”, “false”, and “off”, 转换为Falseo 其他返回ValueError.
items(section) 返回给定section中每个选项的(name,value)对的列表。
import configparser

config = configparser.ConfigParser()

print(config.sections()) # []
# ---------------------------查找文件内容,基于字典的形式
config.read('example1.ini') # ['bitbucket.org', 'topsecret.server.com'] print(config.sections())
print('bytebong.com' in config) # False
print('bitbucket.org' in config) # True print(config['bitbucket.org']["user"]) # hg print(config['DEFAULT']['Compression']) #yes 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'下所有键
# ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11'] print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对
# [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')] print(config.get('bitbucket.org','compression')) # yes get方法Section下的key对应的value
print(config.get('topsecret.server.com','host port')) # for k, v in config.items('bitbucket.org'):
print(k, v)
"""
serveraliveinterval 45
compression yes
forwardx11 yes
user hg
"""

修改和删除文件内容

增加配置文件中的值

方法 描述
add_section(section) 向实例添加一个section

删除配置文件中的值

方法 描述
remove_option(section, option) 从指定的部分中删除指定的选项。如果该部分不存在,请提出NoSectionError。如果存在的选项被删除,返回True;否则返回False。
remove_section(section) 从配置中删除指定的section。如果这个部分确实存在,返回True。否则返回假

修改配置文件中的值

方法 描述
set(section, option, value) 如果给定的部分存在,将给定的选项设置为指定的值
import configparser

config = configparser.ConfigParser()

config.read('example.ini')

config.add_section('db')  # 增加

config.remove_section('bitbucket.org')  # 删除
config.remove_option('topsecret.server.com',"forwardx11") # 删除 config.set('topsecret.server.com','k1','') # 修改
config.set('db','k2','') # 修改 config.write(open('new2.ini', "w"))

Python模块——configparser的更多相关文章

  1. 保留注释换行的python模块configparser

    python语言用来解析配置文件的模块是ConfigParser,python3中是configparser模块,我在使用中发现write方法在将配置项重新写入文 件时,配置文件中的空行和注释行都会被 ...

  2. Python模块configparser(操作配置文件ini)

    configparser模块提供对ini文件的增删改查方法. ini文件的数据格式: [name1] attribute1=value1 attribute2=value2 [name2] attri ...

  3. Python模块 - configparser

    configparser模块用来对配置文件进行操作 1.获取所有块 import configparser config = configparser.ConfigParser() config.re ...

  4. python 模块之-configparser

    python 模块configparser   配置文件模块 import configparser    config = configparser.ConfigParser() config[&q ...

  5. python模块(shelve,xml,configparser,hashlib,logging)

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

  6. python模块基础之json,requeste,xml,configparser,logging,subprocess,shutil。

    1.json模块 json     用于[字符串]和 [python基本数据类型] 间进行转换(可用于不同语言之前转换),json.loads,将字符串转成python的基本数据类型,json.dum ...

  7. Python之配置文件模块 ConfigParser

    写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...

  8. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  9. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

随机推荐

  1. MongoDB:索引操作

    首先插入十万个数据 ;i;i++){ var rand = parseInt(i*Math.random()); db.person_test.insert({"name":&qu ...

  2. GCD - Extreme (II) (欧拉函数妙用)

    https://cn.vjudge.net/problem/UVA-11426 题意:求 解题思路:我们可以定义一个变量dis[n],dis[n]意为1~(n-1)与n的gcd(最大公约数)的总和,那 ...

  3. sql2012包含数据库,快速生成用户tsql脚本

    今天太忙(下班时,发现一个考试网站的不算BUG的BUG,这个BUG刚好能让我找到想要的数据,现在正辛苦的编码中...) 不多说,今天的技术文章,简单一点,帖一段昨天写的SQL代码 用于SQL2012中 ...

  4. 统计百分比的一个SQL脚本

    统计一个表中一个百分比的SQL脚本,不过这个是个万分比,这个数据类型要调一调 ),) declare @num3 decimal,@num4 decimal declare @percent deci ...

  5. List,set,Map理解

    集合 集合与数组 数组(可以存储基本数据类型)是用来存现对象的一种容器,但是数组的长度固定,不适合在对象数量未知的情况下使用. 集合(只能存储对象,对象类型可以不一样)的长度可变,可在多数情况下使用. ...

  6. ios Block详解

    一. iOS代码块Block 1.1 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返回值, ...

  7. OpenCV图像分割1

    1.基于阈值 1.1原理 灰度阈值化,假设输入图像为f,输出图像为g,则阈值化公式如下: g(i,j)=1  当f(i,j)>=T g(i,j)=0 当f(i,j)<T 1.2适用范围 当 ...

  8. ABP框架系列之五十三:(Web-API-Controllers-Web-API-控制器)

    Introduction ASP.NET Boilerplate is integrated to ASP.NET Web API Controllers via Abp.Web.Api nuget ...

  9. openstack之cinder_backup对接ceph存储

    M版openstack,是kolla部署的 1.介绍 backup 功能好像与 snapshot 很相似,都可以保存 volume 的当前状态,以备以后恢复.但二者在用途和实现上还是有区别的,具体表现 ...

  10. 让IE8支持html5中的video标签

    这是一篇综合几个前辈的解决方案. 使用video的时候,要遇到的问题. ①不兼容ie9及其以下版本 在<head>里添加两行, 参考张鑫旭前辈的博客,但是在ie8中薄播放. <!-- ...