configparser模块用于生成和修改常见配置文档。

预制配置文件:conf.ini

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg
MaxUsers = 100 [topsercret.server.com]
Port = 50022
ForwardX11 = no
解析配置文件
>>> import configparser
>>> conf = configparser.ConfigParser() # 准备处理文件
>>> conf.read('conf_ini')
['conf_ini']
>>> print(conf.sections())
['bitbucket.org', 'topsercret.server.com']
>>> print(conf.default_section)
DEFAULT
>>> 'bitbucket.org' in conf
True
>>> conf['bitbucket.org']['User']
'hg'
>>> print(list(conf['bitbucket.org'].keys()))
['user', 'maxusers', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
>>> for k in conf['topsercret.server.com']:
... print(k)
...
port
forwardx11
serveraliveinterval
compression
compressionlevel

  可以发现,每个节点都会默认包含default值。

增删改查

首先准备conf_test.ini文件

[group1]
k1 = v1
k2:v2 [group2]
k1=v1

对conf_test_ini文件进行读和添加操作:

>>> import shutil
>>> shutil.copyfile('conf_ini', 'conf_test.ini')
'conf_test.ini'
# vim conf_test.ini 修改配置文件信息 >>> import ConfigParser
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'ConfigParser'
>>> import configparser
>>> conf = configparser.ConfigParser()
>>> conf.read('conf_test.ini')
['conf_test.ini']
print(dir(conf))
['BOOLEAN_STATES', 'NONSPACECRE', 'OPTCRE', 'OPTCRE_NV', 'SECTCRE', '_DEFAULT_INTERPOLATION',...'set', 'setdefault', 'update', 'values', 'write']
>>> print(conf.options('group1'))
['k1', 'k2']
>>> print(conf['group1']['k2'])
v2
>>> conf.add_section('group3')
>>> conf['group3']['name'] = 'hqs'
>>> conf['group3']['age'] = '' # 不能使用数字
>>> conf.write(open('conf_test.ini', 'w')) # 写入新文件
[group1]
k1 = v1
k2 = v2 [group2]
k1 = v1 [group3]
name = hqs
age = 22

对conf_test_ini文件进行删操作

>>> import configparser
>>> conf = configparser.ConfigParser()
>>> conf.read('conf_test.ini')
['conf_test.ini']
>>> conf.remove_option('group1', 'k2') # 删除某一项
True
>>> conf.write(open('conf_test.ini', 'w')) [group1]
k1 = v1 [group2]
k1 = v1 [group3]
name = hqs
age = 22
>>> import configparser
>>> conf = configparser.ConfigParser()
>>> conf.read('conf_test.ini')
['conf_test.ini']
>>> conf.remove_section('group3') # 删除某一整个章节
True
>>> conf.write(open('conf_test.ini', 'w'))
>>> ^D
MacBook-Pro:Desktop hqs$ cat conf_test.ini
[group1]
k1 = v1 [group2]
k1 = v1

configparser模块——配置文档的更多相关文章

  1. python常用模块-配置文档模块(configparser)

    python常用模块-配置文档模块(configparser) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. ConfigParser模块用于生成和修改常见配置文档,当前模块的名称 ...

  2. configparser模块——用于生成和修改常见配置文档

    配置文档格式 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [b ...

  3. Python学习 :常用模块(四)----- 配置文档

    常用模块(四) 八.configparser 模块 官方介绍:A configuration file consists of sections, lead by a "[section]& ...

  4. Nginx配置文档具体解释

    Nginx的配置文档具体解释.在这儿做个总结,以便以后使用的时间查看. 下面大部分自己整理.部分来自參考 #设置用户 #user  nobody; #启动进程数(一般和server的CPU同样) #能 ...

  5. MYSQL服务器my.cnf配置文档详解

    MYSQL服务器my.cnf配置文档详解 硬件:内存16G [client] port = 3306 socket = /data/3306/mysql.sock [mysql] no-auto-re ...

  6. 转!!Java代码规范、格式化和checkstyle检查配置文档

    为便于规范各位开发人员代码.提高代码质量,研发中心需要启动代码评审机制.为了加快代码评审的速度,减少不必要的时间,可以加入一些代码评审的静态检查工具,另外需要为研发中心配置统一的编码模板和代码格式化模 ...

  7. Hibernate配置文档详解

    Hibernate配置文档有框架总部署文档hibernate.cfg.xml 和映射类的配置文档 ***.hbm.xml hibernate.cfg.xml(文件位置直接放在src源文件夹即可) (在 ...

  8. Java代码规范、格式化和checkstyle检查配置文档

    http://www.blogjava.net/amigoxie/archive/2014/05/31/414287.html 文件下载: http://files.cnblogs.com/files ...

  9. Spring Hibernate4 整合配置文档

    1 applicationContext.xml配置文档 <?xml version="1.0" encoding="UTF-8"?><bea ...

随机推荐

  1. shell-003:用for循环统计内存使用量

    shell-100主要是用于练习! #!/bin/bash # 统计内存的使用量(这里用ps统计) # 第一步:不打印第一行,这里的sed ‘1d’ 去掉 for n in `ps aux |sed ...

  2. sharding-jdbc数据分片配置

    数据分片 不使用Spring 引入Maven依赖 <dependency> <groupId>org.apache.shardingsphere</groupId> ...

  3. Qt 学习之路 2(28):坐标系统

    Qt 学习之路 2(28):坐标系统 豆子 2012年11月25日 Qt 学习之路 2 59条评论 在经历过实际操作,以及前面一节中我们见到的那个translate()函数之后,我们可以详细了解下 Q ...

  4. [BZOJ 3262]陌上开花

    今天写了到偏序问题,发现博主真的是个傻X 传送门 以前的写法 /************************************************************** Probl ...

  5. Subsequence(二分)

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  6. QDU_组队训练(AJFC)

    A - Pretty Matrix DreamGrid's birthday is coming. As his best friend, BaoBao is going to prepare a g ...

  7. wzoi(栈模拟)

    链接:https://ac.nowcoder.com/acm/contest/332/I 来源:牛客网 题目描述 bleaves 最近在 wzoi 上面做题. wzoi 的题目有两种,一种是 noip ...

  8. RPC 框架 应用

    RPC RPC(Remote Procedure Call)服务,也即远程过程调用,在互联网企业技术架构中占据了举足轻重的地位,尤其在当下微服务化逐步成为大中型分布式系统架构的主流背景下,RPC 更扮 ...

  9. Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列

    Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...

  10. 面向对象中的@classonlymethod 与 @classmethod的区别

    如果要使用classonlymethod ,则需要先定义好一个classonlymethod 类. 首先我们需要明白无论是classonlymethod还是classmethod,本质都是一个类,而c ...