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的配置文件路径,此路径为测试环境的路径,线上系统对应配置文件的路径可能不一样,在进行相关配置时,应 ...
随机推荐
- Android笔记--View绘制流程源码分析(二)
Android笔记--View绘制流程源码分析二 通过上一篇View绘制流程源码分析一可以知晓整个绘制流程之前,在activity启动过程中: Window的建立(activit.attach生成), ...
- tsung基准测试方法、理解tsung.xml配置文件、tsung统计报告简介
网上搜集的资料,资料来源于:http://blog.sina.com.cn/ishouke 1.tsung基准测试方法 https://pan.baidu.com/s/1Ne3FYo8XyelnJy8 ...
- Python3获取大量电影信息:调用API
实验室这段时间要采集电影的信息,给出了一个很大的数据集,数据集包含了4000多个电影名,需要我写一个爬虫来爬取电影名对应的电影信息. 其实在实际运作中,根本就不需要爬虫,只需要一点简单的Python基 ...
- Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS
G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...
- Ajax经典的面试题
1.什么是AJAX,为什么要使用Ajax(请谈一下你对Ajax的认识)什么是ajax:AJAX是“Asynchronous JavaScript and XML”的缩写.他是指一种创建交互式网页应用的 ...
- Android(java)学习笔记113:Activity的生命周期
1.首先来一张生命周期的总图: onCreate():创建Acitivity界面 onStart():让上面创建的界面可见 onResume():让上面创建的界面 ...
- Beta版本发布
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/?page=2 这个作业要求在哪里 <作业要求的 ...
- matlab启动
直接在命令行输入matlab会报错 用这两个命令没问题 sudo /usr/local/MATLAB/R2013a/bin/matlab sudo /usr/local/MATLAB/R2013a/b ...
- string 的用法
上次,我在" Anton And Danik "中为大家介绍了 string 的部分用法 今天,我就再来为大家介绍一下 string 的其他用法 : ( 有可能已经讲过了,不要介意 ...
- sort 与 sorted 区别:
sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值, ...