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. python定义函数时的默认返回值

    python定义函数时,一般都会有指定返回值,如果没有显式指定返回值,那么python就会默认返回值为None, 即隐式返回语句: return None 执行如下代码 def now(): prin ...

  2. nmon工具

    下载地址 1.http://nmon.sourceforge.net/pmwiki.php?n=Site.Download tar xvzf  *.tar.gz,需要配置权限 chmod -x,同时配 ...

  3. XE10 塔建 Android 开发环境 (已测试通过)

    XE10 塔建 Android 开发环境 1. E:\DevCSS\Android\adt-bundle-windows-x86-20131030\adt-bundle-windows-x86-201 ...

  4. 多线程sshd爆破程序代码

    不多说了,直接上手代码,也没有啥练手的,都是很熟悉的代码,水一篇,方便作为工作的小工具吧.试了一下,配合一个好点的字典,还是可以作为个人小工具使用的. #!/usr/bin/env python # ...

  5. 20165330 2017-2018-2 《Java程序设计》第8周学习总结

    课本知识总结 第十二章 Java多线程机制 Java中的线程 进程:是程序的一次动态执行过程,它对应了从代码加载.执行至执行完毕的一个完整过程 线程:一个进程在其执行过程中,可以产生多个线程,形成多条 ...

  6. qt model view 编程总结

    看不见的root的 QModelIndex() 是 无效的 list 和table 的index 函数中的parent参数就只要 root QModelIndex 就可以,因为没有层级概念 Model ...

  7. Cookies Client Identification

    HTTP The Definitive Guide Cookies are the best current way to identify users and allow persistent se ...

  8. 【react router路由】<Router> <Siwtch> <Route>标签

    博客 https://www.jianshu.com/p/ed5e56994f13?from=timeline 文档 http://react-guide.github.io/react-router ...

  9. linux知识体系

    0. Linux简介与厂商版本 1. Linux开机启动 2. Linux文件管理 3. Linux的架构 4. Linux命令行与命令 5. Linux文件管理相关命令 6. Linux文本流 7. ...

  10. 2018 又一个假期 over 了

    五一假期三天  在家窝三天  早上起床仍感觉没休息好  其实确实没休息好  假期的时间总不愿意拿来补觉用.周末准备去体检,真心恐惧医院的味道,硬着头皮还得上啊.