用于生成和修改常见配置文档,当前模块的名称在 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. Fitnesse框架简单介绍

    1.Fitnesse是什么? 官方的说明:FitNesse is a wiki server. It's also a test execution engine. Fitnesse是一个wiki s ...

  2. 数据库路由中间件MyCat - 源代码篇(13)

    此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 4.配置模块 4.2 schema.xml 接上一篇,接下来载入每个schema的配置(也就是每个MyCat ...

  3. cordova之旅之初识

    emmmm, 一直徘徊在移动端采用什么技术比较好,一直也没有找到,让我为了一个移动端而去学习一波react全家桶是不现实的操作,反观自己的技术栈,通过长时间的对比和剖析找到了入口点,不管了先会写再说吧 ...

  4. 洛谷P4213 【模板】杜教筛(Sum)(杜教筛,莫比乌斯反演)

    传送门 坑着,联赛活着回来再填(死了就不填了) // luogu-judger-enable-o2 //minamoto #include<iostream> #include<cs ...

  5. 获取URL地址栏参数(正则表达式)

    ]] = isEncode ? decodeURIComponent(arg[2]) : arg[2]; }); return obj;}

  6. Linux服务器上的禅道迁移及升级方法(Linux to Linux)

    由于阿里云服务器(Linux系统)到期停用,故需要将部署在该服务器上的禅道迁移到新的Linux服务器上.另外,借此机会,正好可以对旧版禅道进行一次升级.下面总结此次迁移和升级的具体操作方法. 一.禅道 ...

  7. 黑马学习AJAX jQuery发送异步请求 $.ajax() $.post() $.get()是在调用方法而不是定义方法

  8. 2、CreateJS介绍-TweenJS

    需要在html5文件中引入的CreateJS库文件是easeljs-0.7.1.min.js和tweenjs-0.5.1.min.js HTML5文件如下: <!DOCTYPE html> ...

  9. Linux 下 FTP虚拟用户的使用配置

    Linux下FTP虚拟用户的使用配置 Linux的FTP服务支持3种用户: 1.匿名帐户 2.本地帐户 3.虚拟用户 为什么要使用虚拟用户: 匿名帐户可以很好的保证FTP服务器的安全性,但是,对匿名用 ...

  10. 一个VUE的小案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...