configparser模块来生成和修改配置文件
1. 安装configparser模块
pip3 install configparser ##python2.7模块名为ConfigParser
2. 创建配置文件
import configparser
config = configparser.ConfigParser()
config['DEFAULT'] = {
'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': ''
}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
config['topsecret.server.com']['Host Port'] = '' # mutates the parser
config['topsecret.server.com']['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)
3. 读取配置以及修改
说明:
read(filename) 直接读取ini文件内容
sections() 得到所有的section,并以列表的形式返回
options(section) 得到该section的所有option
items(section) 得到该section的所有键值对
add_section(section) 添加一个新的section
set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
例子:
import configparser conf = configparser.ConfigParser()
conf.read("example.ini") print(conf.defaults()) ## 打印默认的配置
print(conf['bitbucket.org']['user']) ## 查看sections下面options的value
conf.set("bitbucket.org", "user", "my") ## 修改配置文件
print(conf.sections()) ## 获取配置文件下所有的sections
print(conf.options("topsecret.server.com")) ## 获取该sections下面的所有options
print(conf.items("topsecret.server.com")) ## 获取该sections下面所有options键值对 conf.add_section('doubi') ## 添加一个sections
conf.set("doubi", "name", "wangzai") ## 添加sections的options,并赋值 sec = conf.remove_section('topsecret.server.com') ## 删除该sections
conf.write(open('example1.ini', "w")) ## 把上面所有的操作写入到exapmle1.ini文件中
configparser模块来生成和修改配置文件的更多相关文章
- configparser模块——用于生成和修改常见配置文档
配置文档格式 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [b ...
- 使用configparser模块进行封装,构造配置文件处理器
from configparser import ConfigParser class HandleConfig: ''' 定义一个配置文件处理类 ''' def __init__(self, fil ...
- python 配置文件 ConfigParser模块
ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 [DEFAULT] Se ...
- python中configparser模块的使用
configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...
- PyYAML和configparser模块讲解
Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation ymal主要用于配置文件. Co ...
- xml和configparser模块
一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, 但至今很多传统公司如金融行业的很多系统的接口还主要是xml. xml的格式如下,就是通过 ...
- configparser模块——配置文档
configparser模块用于生成和修改常见配置文档. 预制配置文件:conf.ini [DEFAULT] ServerAliveInterval = 45 Compression = yes Co ...
- Python学习笔记——基础篇【第六周】——PyYAML & configparser模块
PyYAML模块 Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation 常用模块之Co ...
- python学习第五十三天configParser模块的使用
configParser 模块用于生成和修改常见配置文档,python 3.x为configParser,配置软件的常见配置格式 模块的用法 import configparser config=co ...
随机推荐
- Math函数
floor --将一个小数向下舍入为整数 float floor ( float $value ) 注意:floor返回的虽然是取整的数字 但是类型仍然是float类型. 实例: echo floor ...
- Spring_day03--课程安排_基于aspectj的注解aop_Spring的jdbcTemplate操作
Spring_day03 上节内容回顾 今天内容介绍 基于aspectj的注解aop Spring的jdbcTemplate操作 增加 修改 删除 查询 Spring配置c3p0连接池和dao使用jd ...
- 系统管理模块_部门管理_改进_抽取添加与修改JSP页面中的公共代码_在显示层抽取BaseAction_合并Service层与Dao层
系统管理模块_部门管理_改进1:抽取添加与修改JSP页面中的公共代码 commons.jspf <%@ page language="java" import="j ...
- WEB安全番外第三篇--关于XXE
一.什么是XXE 1.XML实体简介 (1)在一段时间中,XML都是WEB信息传输的主要方法,时至今日XML在WEB中作为前后台之间传递数据的结构,依然发挥着重要的作用.在XML中有一种结构叫做实体: ...
- LeetCode 笔记系列二 Container With Most Water
题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- CodeForces 651 A Joysticks
A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Java23种设计模式学习笔记【目录总贴】
创建型模式:关注对象的创建过程 1.单例模式:保证一个类只有一个实例,并且提供一个访问该实例的全局访问点 主要: 饿汉式(线程安全,调用效率高,但是不能延时加载) 懒汉式(线程安全,调用效率不高,但 ...
- ArcGIS Server管理工具之批量发布动态地图服务工具.md
友好阅读链接:(http://swj.me/2015/08/26/batchPublishtools/) update0918: 修复了创建链接文件时而出错的bug 修复了在24011的警告已处理的情 ...
- King's Game---hdu5643(约瑟夫环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5643 约瑟夫环问题的原来描述为,设有编号为1,2,……,n的n(n>0)个人围成一个圈,从 ...
- Android 查看自己的keystore的别名及相关信息
1.在DOS窗口下进入自己的keystore所在位置,输入 keytool -list -v -keystore xxxx.keystore -storepass 密码 xxxx.keystore是 ...