用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。在python2.x版本中为ConfigPsresr

来看一个好多软件的常见文档格式如下

[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [bitbucket.org]
user = hg [topsecret.server.com]
host port = 50022
forwardx11 = no

如果想用python生成一个这样的文档怎么做呢?

import configparser
config=configparser.ConfigParser()#生成一个处理对象
config['DEFAULT']={'ServerAliveInterval':'45',
'Compression':'yes',
'CompressionLevel':'9'} config['bitbucket.org']={}
config['bitbucket.org']['User']='hg' config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'
topsecret['ForwardX11'] = 'no' config['DEFAULT']['ForwardX11'] = 'yes' with open('example.ini','w') as configfile:
config.write(configfile)

写完了还可以再读出来

import configparser
config=configparser.ConfigParser()#生成一个处理对象
config.read('example.ini')
print(config.defaults())
print(config.sections())
print('bitbucket.org' in config)
print('bytebong.com' in config)
print(config['bitbucket.org'])
print(config['bitbucket.org']['user'])
topsecret=config['topsecret.server.com']
print(topsecret['ForwardX11'])
for key in config['topsecret.server.com']: print(key)
import configparser
config=configparser.ConfigParser()#生成一个处理对象
config.read('example.ini')
options = config.options('topsecret.server.com')
print(options)
item_list = config.items('DEFAULT')
print(item_list)
val=config.get('bitbucket.org','user')
print(val)

configparser增删改查语法

import configparser
config=configparser.ConfigParser()#生成一个处理对象
config.read('example.ini') sec=config.remove_section('bitbucket.org')
config.write(open('example1.cfg', "w")) print(config.has_section('topsecret.server.com'))
print(config.has_section('server.com')) sec=config.add_section('wupeiqi')
config.write(open('example2.cfg', "w")) config.remove_option('topsecret.server.com','forwardx11')
config.write(open('example3.cfg', "w"))

http://www.cnblogs.com/ming5218/p/7965973.html

python_89_configparser模块的更多相关文章

  1. npm 私有模块的管理使用

    你可以使用 NPM 命令行工具来管理你在 NPM 仓库的私有模块代码,这使得在项目中使用公共模块变的更加方便. 开始前的工作 你需要一个 2.7.0 以上版本的 npm ,并且需要有一个可以登陆 np ...

  2. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  3. ES6模块import细节

    写在前面,目前浏览器对ES6的import支持还不是很好,需要用bable转译. ES6引入外部模块分两种情况: 1.导入外部的变量或函数等: import {firstName, lastName, ...

  4. Python标准模块--ContextManager

    1 模块简介 在数年前,Python 2.5 加入了一个非常特殊的关键字,就是with.with语句允许开发者创建上下文管理器.什么是上下文管理器?上下文管理器就是允许你可以自动地开始和结束一些事情. ...

  5. Python标准模块--Unicode

    1 模块简介 Python 3中最大的变化之一就是删除了Unicode类型.在Python 2中,有str类型和unicode类型,例如, Python 2.7.6 (default, Oct 26 ...

  6. Python标准模块--Iterators和Generators

    1 模块简介 当你开始使用Python编程时,你或许已经使用了iterators(迭代器)和generators(生成器),你当时可能并没有意识到.在本篇博文中,我们将会学习迭代器和生成器是什么.当然 ...

  7. 自己实现一个javascript事件模块

    nodejs中的事件模块 nodejs中有一个events模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...

  8. 理解nodejs模块的scope

    描述 原文档地址:https://docs.npmjs.com/misc/scope 所有npm模块都有name,有的模块的name还有scope.scope的命名规则和name差不多,同样不能有ur ...

  9. nodejs模块发布及命令行程序开发

    前置技能 npm工具为nodejs提供了一个模块和管理程序模块依赖的机制,当我们希望把模块贡献出去给他人使用时,可以把我们的程序发布到npm提供的公共仓库中,为了方便模块的管理,npm规定要使用一个叫 ...

随机推荐

  1. SCUT - 299 - Kaildls的数组划分 - dp - 高精

    https://scut.online/p/299 \(dp[i][k]\) 为前 \(i\) 个数分 \(k\) 组的最大值,那么 $dp[i][k]=max_{p=1}^{i-1}{dp[p][k ...

  2. 仿iPhone、iPad界面滑屏切换

    <!DOCTYPE html> <html lange='en'> <head> <meta charset='UTF-8'> <title> ...

  3. ue4 character 物理测试

    charactor里 CapsuleComponnet Mesh CharacterMovement 3个组件里有有物理开关 目前看Mesh的Simulate Physics+Enable Gravi ...

  4. windows 10 删除库后自动恢复的解决方法

    目录 什么是windows 库? 手动删除不行吗? 如何正确的"删除"? title: windows 10 删除库后自动恢复的解决方法 date: 2019-06-09 15:4 ...

  5. Corn Fields(模板)

    题目链接 #include <stdio.h> #include <algorithm> #include <string.h> #include <iost ...

  6. Python-14-抽象及关键字参数

    可使用内置函数callable判断某个对象是否可调用 >>> import math >>> x = 1 >>> y = math.sqrt &g ...

  7. .NET 基础 一步步 一幕幕[XML基础操作]

    XML可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. 什么是XML,学他有什么用? 优点:容易读懂,格式标准任何语言都内置了XML分析引擎,不用单独进行文件分 ...

  8. phpcms9.6 注入分析

    phpcms9.6 注入分析 漏洞促发点\phpcms\modules\content\down.php $a_k = trim($_GET['a_k']); if(!isset($a_k)) sho ...

  9. 牛客假日团队赛2 F.跳跃

    链接: https://ac.nowcoder.com/acm/contest/924/F 题意: Farmer John为了满足奶牛对美的享受而安装了人工湖.矩形的人工湖分成M行N列(1 <= ...

  10. BZOJ 1059(二分图匹配)

    要点 发现每行每列都得有1 发现无论怎么换,在同一行的永远在同一行,同一列的永远在同一列 于是换行貌似没什么用啊,换列就够了.换列无法做到则无答案 于是变成了行与列进行二分匹配 #include &l ...