python_89_configparser模块
用于生成和修改常见配置文档,当前模块的名称在 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模块的更多相关文章
- npm 私有模块的管理使用
你可以使用 NPM 命令行工具来管理你在 NPM 仓库的私有模块代码,这使得在项目中使用公共模块变的更加方便. 开始前的工作 你需要一个 2.7.0 以上版本的 npm ,并且需要有一个可以登陆 np ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- ES6模块import细节
写在前面,目前浏览器对ES6的import支持还不是很好,需要用bable转译. ES6引入外部模块分两种情况: 1.导入外部的变量或函数等: import {firstName, lastName, ...
- Python标准模块--ContextManager
1 模块简介 在数年前,Python 2.5 加入了一个非常特殊的关键字,就是with.with语句允许开发者创建上下文管理器.什么是上下文管理器?上下文管理器就是允许你可以自动地开始和结束一些事情. ...
- Python标准模块--Unicode
1 模块简介 Python 3中最大的变化之一就是删除了Unicode类型.在Python 2中,有str类型和unicode类型,例如, Python 2.7.6 (default, Oct 26 ...
- Python标准模块--Iterators和Generators
1 模块简介 当你开始使用Python编程时,你或许已经使用了iterators(迭代器)和generators(生成器),你当时可能并没有意识到.在本篇博文中,我们将会学习迭代器和生成器是什么.当然 ...
- 自己实现一个javascript事件模块
nodejs中的事件模块 nodejs中有一个events模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...
- 理解nodejs模块的scope
描述 原文档地址:https://docs.npmjs.com/misc/scope 所有npm模块都有name,有的模块的name还有scope.scope的命名规则和name差不多,同样不能有ur ...
- nodejs模块发布及命令行程序开发
前置技能 npm工具为nodejs提供了一个模块和管理程序模块依赖的机制,当我们希望把模块贡献出去给他人使用时,可以把我们的程序发布到npm提供的公共仓库中,为了方便模块的管理,npm规定要使用一个叫 ...
随机推荐
- MYSQL数据库设计规范11111
MYSQL数据库设计规范 1.数据库命名规范 采用26个英文字母(区分大小写)和0-9的自然数(经常不需要)加上下划线'_'组成; 命名简洁明确(长度不能超 ...
- MySQL下载与安装配置
一.Windows 上安装 MySQL Windows 上安装 MySQL 相对简单,最新版本下载地址: 官网:https://dev.mysql.com/downloads/mysql/ 下载步骤: ...
- Centos 7 install cacti监控
首先,先安装LNMP服务 安装一: 如果觉得安装起来麻烦,可以到如下网站进行安装: https://lnmp.org/install.html 安装二: 采用yum或者安装包的方式进行安装,具体操作请 ...
- 11.联结表---SQL
说明:使用交互式DBMS工具重要的是,要理解联结不是物理实体.换句话说,它在实际的数据库表中并不存在.DBMS会根据需要建立联结,它在查询执行期间一直存在. 一.等值语法:SELECT 字段 FROM ...
- Python多继承C3算法
Python3 多继承的MRO算法选择.MRO(Method Resolution Order):方法解析顺序. Python3 只保留了C3算法! C3算法解析: 1.C3算法解析 C3算法:MRO ...
- 命令行 IRC 客户端 irssi 的基本操作
登录与退出 启动 irssi $ irssi 登陆 IRC 服务器/connect server port 修改昵称/nick nickname 直接带昵称登录指定的服务器$ irssi -c [se ...
- TDH-大数据基础
------------------------------------------------------------------------------------*******大数据概念和基础* ...
- ios中 input 焦点光标不垂直居中
笔记:在ios,如果同时给input设置这种平时我们使字体垂直居中的css写法. 光标会出现,如下图的问题 . 改正方案: 采取不使用line-height的垂直居中方法即可.
- vue or react mvvm里的文字上下滚动
1.jQuery 时候实现 上下滚动很简单,基本上一个animateTop就可以了 2. vue等MVVM就有些麻烦了,因为不推荐操作DOM,专注于数据 我们可以使用 css3 transition: ...
- CSS中垂直水平居中
方法一:使用flex布局,父级元素设置justify-content和align-items <div class="cont"> <div class=&quo ...