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模块来生成和修改配置文件的更多相关文章

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

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

  2. 使用configparser模块进行封装,构造配置文件处理器

    from configparser import ConfigParser class HandleConfig: ''' 定义一个配置文件处理类 ''' def __init__(self, fil ...

  3. python 配置文件 ConfigParser模块

    ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 [DEFAULT] Se ...

  4. python中configparser模块的使用

    configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...

  5. PyYAML和configparser模块讲解

    Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation ymal主要用于配置文件. Co ...

  6. xml和configparser模块

    一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, 但至今很多传统公司如金融行业的很多系统的接口还主要是xml. xml的格式如下,就是通过 ...

  7. configparser模块——配置文档

    configparser模块用于生成和修改常见配置文档. 预制配置文件:conf.ini [DEFAULT] ServerAliveInterval = 45 Compression = yes Co ...

  8. Python学习笔记——基础篇【第六周】——PyYAML & configparser模块

    PyYAML模块 Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation 常用模块之Co ...

  9. python学习第五十三天configParser模块的使用

    configParser 模块用于生成和修改常见配置文档,python 3.x为configParser,配置软件的常见配置格式 模块的用法 import configparser config=co ...

随机推荐

  1. 概览C++之const

    1.C语言中const与 C++中的const void main() { const int a = 10; int *p = (int*)&a; *p = 20; printf(" ...

  2. oracle 在sql中拼接时间

    <isNotEmpty prepend="and" property="startDate"> to_date(rs.count_date, 'YY ...

  3. tinker

    Ios前一段时间因为热更新被强制下架也算是最大闻了,但Android没关系,继续玩 首先tinker比Andfix好多了,版本现在都到1.7.11了,Andfix不支持yunos, 现在项目中没有用到 ...

  4. Codeforces Round #359 (Div. 2) C. Robbers' watch 搜索

    题目链接:http://codeforces.com/contest/686/problem/C题目大意:给你两个十进制的数n和m,选一个范围在[0,n)的整数a,选一个范围在[0,m)的整数b,要求 ...

  5. 移动端form表单

    始终绑定submit事件 不单独的对[提交]按钮绑定click事件,对整个表单绑定submit提交事件,这样可以让整个表单内的文本框获得Enter提交的VIP待遇,并且在移动端中可以让文本框聚焦时键盘 ...

  6. Map中存放数组

    Map<String,Object> map = new HashMap<String, Object>(); Map<String,String> agentMa ...

  7. 数据集划分——train set, validate set and test set

    先扯点闲篇儿,直取干货者,可以点击这里. 我曾误打误撞的搞过一年多的量化交易,期间尝试过做价格和涨跌的预测,当时全凭一腔热血,拿到行情数据就迫不及待地开始测试各种算法. 最基本的算法是技术指标类型的, ...

  8. python线程池ThreadPoolExecutor用法

    线程池concurrent.futures.ThreadPoolExecutor模板 import time from concurrent.futures import ThreadPoolExec ...

  9. 160331、使用@Controller注解为什么要配置<mvc:annotation-driven />

    为了解决静态资源访问的问题,servlet改成了拦截所有请求,即/,并添加了默认的servlet,这时候*.do请求不能被控制器捕捉了,页面错误为404.直到添加了<mvc:annotation ...

  10. 2017 Multi-University Training Contest - Team 8

    HDU6140 Hybrid Crystals 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6140 题目意思:这场多校是真的坑,题目爆长,心态爆炸, ...