python 模块之-configparser
python 模块configparser 配置文件模块
import configparser
config = configparser.ConfigParser()config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {'User': 'hg'}config['topsecret.server.com'] = {'Host Port':'50022', 'ForwardX11','no'}with open('example.ini', 'w') as configfile: #把键值对写入 配置文件example.ini config.write(configfile)[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no
config.read('example.ini') # 关联文件才能操作里面键值
print(config.sections()) #['bitbucket.org', 'topsecret.server.com'] 查看段
print('bytebong.com' in config)# False 判断
print(config['bitbucket.org']['User']) # hg 查看值
print(config['DEFAULT']['Compression']) #yes 查看值
print(config['topsecret.server.com']['ForwardX11']) #no 查看值
for key in config['bitbucket.org']: #循环打印[bitbucker.org]的键
print(key)
###打印段[bitbucker.org]的所有键
print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
###循环打印段[bitbucker.org]的所有键值
print(config.items('bitbucket.org')) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
###打印段[bitbucket.org] 键compression 的值
print(config.get('bitbucket.org','compression'))#yes
config.add_section('yuan') #增加一个段 [yuan]
config.remove_section('topsecret.server.com') # 删除一个段[topsecret.server.com]
config.remove_option('bitbucket.org','user') # 删除这个[bitbucket.org]段里面的键和值User = hg
config.set('bitbucket.org','k1','11111') # 添加[bitbucket.org]段里面 键值 k1 = 11111
config.write(open('i.cfg', "w") # 写入的话 必须先执行这条命令
python 模块之-configparser的更多相关文章
- Python模块之: ConfigParser 配置文件读取
Python模块之: ConfigParser 配置文件读取 ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型. ...
- Python模块学习 - ConfigParser
配置文件 很多软件都用到了配置文件,像git运行的时候会读取~/gitconfig,MySQL运行的时候会读取/etc/my.cnf,Python 提供的包管理工具pip命令,也会去读取~/.pip/ ...
- Python模块之ConfigParser - 读写配置文件
Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...
- Python 模块续 configparser、shutil、XML、paramiko、系统命令、
一.configparse # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section2] # 节点 k1 = v1 # 值 1.获取所有节点 ...
- Python模块:configparser、hashlib、(subprocess)
configparser模块: 此模块用于生成和修改常见配置文档. 一个常见配置文件(.ini的后缀名)格式如下: [DEFAULT] # DEFAULT 是指后面的字典里都会默认有的内容 Serve ...
- python模块之configparser
configparser用于处理特定格式的文件,其本质上是利用open来操作文件. # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section ...
- python模块:configparser
"""Configuration file parser. A configuration file consists of sections, lead by a &q ...
- python模块之ConfigParser: 用python解析配置文件
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...
- Python 模块之 ConfigParser: 用 Python 解析配置文件
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在 Python 里更是如此,在官方发布的库中就包含有做这件事情的库,那就是 ConfigParser,这里简单的做 ...
随机推荐
- liunx基础知识
学习Linux系统的重要性应该不用多说,下面我就对Linux的基础知识进行一个全面而又简单的总结.不过建议大家还是装个Linux系统多练习,平时最好只在Linux环境下编程,这样会大有提高. linu ...
- Android TimeAnimator && TimeListener翻译
TimeAnimator:提供了一个简单的回调机制,通过 TimeAnimator.TimeListener,在动画的每一帧处通知你.这个动画器没有时间,插值或是对象值设定.回调监听器为每一帧动画接受 ...
- linux中断源码分析 - 软中断(四)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 在上一篇文章中,我们看到中断实际分为了两个部分,俗称就是一部分是硬中断,一部分是软中断.软中断是专门用于处理中断 ...
- highcharts中把X轴的名字竖着显示
Highcharts.chart('container', { chart: { type: 'column' }, title: { text: 'Auto rotation limit' }, s ...
- php利用自定义key,对数据加解密的方法
客户端和服务端通信时,有个场景很常见,通过一个id作为url参数来回传递.假设现在业务上只有这个id标识,那么需要稍微安全一点的通信,对这个id进行加密传输,到服务端再进行解密.这里需要一个服务端进行 ...
- Android自动化测试之:获取 参数:comonentName 的值方法
十年河东十年河西,莫欺少年穷! 不了解Activity的,可参考:http://www.cnblogs.com/tekkaman/archive/2011/06/07/2074211.html 相关代 ...
- Spring Aop: 关于继承和execution target this @annotation
1.多态 target指通过这个对象调用的方法 (匹配标识对象的所有方法) getMethod() this指调用这个对象的方法 (匹配标识对象实现的方法) getDeclaredMethod( ...
- linux下日志文件error监控报警脚本分享
即对日志文件中的error进行监控,当日志文件中出现error关键字时,即可报警!(grep -i error 不区分大小写进行搜索"error"关键字,但是会将包含error大小 ...
- part 1
注意:本次源码分析选择2.0.3(因为不支持IE6.7.8,就少了很多兼容的hack的写法,对了解jQuery的实现原理有很大的帮助) 1.jQuery有不同的版本,从2.x版本便不再支持IE6.7. ...
- 浏览器跨域请求之credentials
-时间起源- 前段时间,需要弄个简单的网站出来,访问远程的api服务. 我是这么做的.首先是在搭建一个nodejs服务来运行前端页面.在我请求登录的时候,能成功返回相应的成功信息.然后,当我再次请求读 ...