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. js获取表单数据

    var modelObj = {}; var modelFieldsArray = $('#AddMusicCategory').serializeArray(); $.each(modelField ...

  2. jedispool 连 redis

    java端在使用jedispool 连接redis的时候,在高并发的时候经常卡死,或报连接异常,JedisConnectionException,或者getResource 异常等各种问题 在使用je ...

  3. GO项目目录

    |--bin |--pkg |--src 其中,bin存放编译后的可执行文件:pkg存放编译后的包文件:src存放项目源文件.一般,bin和pkg目录可以不创建,go命令会自动创建(如 go inst ...

  4. 【BZOJ2525】[Poi2011]Dynamite 二分+树形DP

    [BZOJ2525][Poi2011]Dynamite Description Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了炸.药,现在需要点燃M个点上的引线引爆所有 ...

  5. spring的@Transactional注解详细用法(转载)

    概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framework对事务管理提供了一致的抽象,其特点如下: 为不同的事务API提供一致的编程模型, ...

  6. open live writer 代码着色插件

    插件地址1: http://files.cnblogs.com/files/zsy/OpenLiveWriter.CNBlogs.SourceCode.zip 下载,解压,把里面的文件放到最新的 ap ...

  7. DocumentFragment 不支持 innerHTML

    在需要多次使用 innerHTML 的地方,一般是推荐用 DocumentFragment 来缓存,最后一次性插入 body,从而减少浏览器的渲染,提高性能,不过最近也发现一个 bug: Docume ...

  8. Python--进阶处理5

    # 使用路径名来获取文件名,目录名,绝对路径等import ospath = r'F:\myProject\lean\a'# Get the last component of the pathbas ...

  9. UVALive 6933 Virus synthesis(回文树)

    Viruses are usually bad for your health. How about ghting them with... other viruses? In this proble ...

  10. .net core 启动域名及端口配置

    前两天转载一篇.net core 启动分析,由于发布时候一直纠结在默认5000端口上,所以好好研究了一下. 1.IIS集成 如果通过IIS当宿主的话,那这些都不是事情,强大的IIS可以帮助我们对站点的 ...