configparser模块——用于生成和修改常见配置文档
- 配置文档格式
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg配置文档文件格式
- 解析配置文件:查询
#-*- coding:utf-8 -*-
#解析配置文件
import configparser
config = configparser.ConfigParser()
print(config.sections())#[],此时打印为空,因为还没有读文件呢
config.read("example.ini")
print(config.sections())#打印节点['bitbucket.org', 'topsecret.server.com']
if 'bitbucket.org' in config: #判断该节点是否在配置文件中
print('true')
print(config['bitbucket.org']['User'])#hg,查询value值,操作方式和字典一样
print(config['DEFAULT']['Compression'])#yes,查询value值,操作方式和字典一样
topsecret = config['topsecret.server.com']
print(topsecret['ForwardX11'])#no
print(topsecret['Port'])# for key in config['bitbucket.org']:#用for循环打印,除打印除'bitbucket.org'内的key,也会打印出'default’中的key,
# 'default'中的内容是默认每个节点模块共享的,这样可以避免重复写
print(key)#user serveraliveinterval compression compressionlevel forwardx11解析配置文件
- 其他增删改查语法
import configparser
config = configparser.ConfigParser()
config.read('example.ini')
secs = config.sections()#获取节点
print(secs)#['bitbucket.org', 'topsecret.server.com']
options = config.options('bitbucket.org')#获取某一节点下的KEY
print(options)#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
item_list = config.items('bitbucket.org')#获取某一节点下的KEY,value
print(item_list)#[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
val = config.get('bitbucket.org','serveraliveinterval')
print(val)#45,str格式
val = config.getint('bitbucket.org','serveraliveinterval')
print(val)#45,int格式读
import configparser
config = configparser.ConfigParser()
config.read('example.ini')
#remove section
# sec = config.remove_section('bitbucket.org')#移除'bitbucket.org'节点
# print(sec)##True:移除成功,False:移除失败(内容不存在)
# config.write(open('example.ini','w'))#写
#Add section
# sec = config.has_section('group1')#判断该节点是否存在
# print(sec)#False:不存在,True:存在
# sec = config.add_section('group1')#添加,如果已经存在,会报错
# config.write(open('example.ini','w')) #add option
# config.set('group1','K1','11111')#给group1节点添加内容,如果group1不存在,会报错
# config.write(open('example.ini','w')) #remove option
# sec = config.remove_option('group1','K1')#移除option
# print(sec)#True:移除成功,False:移除失败(内容不存在)
# config.write(open('example.ini','w'))remove,add
configparser模块——用于生成和修改常见配置文档的更多相关文章
- configparser模块来生成和修改配置文件
1. 安装configparser模块 pip3 install configparser ##python2.7模块名为ConfigParser 2. 创建配置文件 import configpar ...
- python常用模块-配置文档模块(configparser)
python常用模块-配置文档模块(configparser) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. ConfigParser模块用于生成和修改常见配置文档,当前模块的名称 ...
- configparser模块——配置文档
configparser模块用于生成和修改常见配置文档. 预制配置文件:conf.ini [DEFAULT] ServerAliveInterval = 45 Compression = yes Co ...
- Python学习 :常用模块(四)----- 配置文档
常用模块(四) 八.configparser 模块 官方介绍:A configuration file consists of sections, lead by a "[section]& ...
- 转!!Java代码规范、格式化和checkstyle检查配置文档
为便于规范各位开发人员代码.提高代码质量,研发中心需要启动代码评审机制.为了加快代码评审的速度,减少不必要的时间,可以加入一些代码评审的静态检查工具,另外需要为研发中心配置统一的编码模板和代码格式化模 ...
- Java代码规范、格式化和checkstyle检查配置文档
http://www.blogjava.net/amigoxie/archive/2014/05/31/414287.html 文件下载: http://files.cnblogs.com/files ...
- azkaban编译安装配置文档
azkaban编译安装配置文档 参考官方文档: http://azkaban.github.io/azkaban/docs/latest/ azkaban的配置文件说明:http://azkaban. ...
- nginx 安全配置文档
1.配置文档中有多处明确写出了nginx的配置文件路径,该路径是测试环境中的路径,线上系统的nginx配置文件与文档中所写的路径可能不一样,在进行相关配置时,应以线上配置文件的实际路径为准. 线上系统 ...
- tomcat 安全配置文档
1.配置文档中使用$CATALINA_HOME变量声明为tomcat的安装目录并明确写出了tomcat的配置文件路径,此路径为测试环境的路径,线上系统对应配置文件的路径可能不一样,在进行相关配置时,应 ...
随机推荐
- null、undefined和NaN的区别
未定义的值和定义未赋值的值是undefined: null是一种特殊的Object,可以给变量赋一个值null,来清除变量的值: NaN是一种特殊的number:
- 如何提高Mysql的查询效率???
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- BZOJ 2119: 股市的预测 SA
2119: 股市的预测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 434 Solved: 200[Submit][Status][Discuss ...
- UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)
大体思路是从终点反向做一次BFS得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...
- codeforce Gym 100418K Cards (概率,数学)
题意:麦田的故事,n张牌,取x张牌,记住前x张牌最大的值m,继续往后取,遇到第一张比m大的牌就停下来.求一个x使得最后的牌在整副牌里是最大的期望最大. 假设最大的牌是A,A在各种位置出现的概率就是相等 ...
- codeforce Gym 100500H ICPC Quest (简单dp)
题意:给一个nXm的矩阵,上面有一些数字,从左上角出发,每次只能往右或者往下,把沿途的数字加起来,求到达右下角的最大值是多少. 题解:简单的一个dp,设f[i][j]为到达i行j列的最大值,f[i][ ...
- [机器视觉] SIFT特征-尺度不变特征理解
SIFT特征-尺度不变特征理解 简介 SIFT,即尺度不变特征变换(Scale-invariant feature transform,SIFT),是用于图像处理领域的一种描述.这种描述具有尺度不变性 ...
- IOS7的变化
API变化: 1.弃用 MKOverlayView 及其子类,使用类 MKOverlayRenderer: 2.弃用 Audio Toolbox framework 中的 AudioSession A ...
- ARC中__weak;__strong;__unsafe_unretained;修饰词
测试代码: // Human.h代码 @interface Human : NSObject @property (nonatomic, weak) Cat *pinkCat; @property ( ...
- UIViewController 的 edgesForExtendedLayout、automaticallyAdjustsScrollViewInsets属性
1.有时你命名设置了某控件的y坐标为0,确总是被导航栏遮挡住,如下: UILabel *label = [[UILabel alloc] init]; label.text = @"请 ...