解析配置文件ConfigParser模块
一.ConfigParser简介
ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。
[mongoDb] #-------->section
userName=dsg
passWord=dsg
dataBase=promo
tableName=trace
mongodb_ip_port=127.0.0.1:3717 [filePath]#-------->section
incoming=/mnt/data/share/ingest/incoming/mongo
中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
二、ConfigParser 初始工作
使用ConfigParser 首选需要初始化实例,并读取配置文件:
import ConfigParser
cf = ConfigParser.ConfigParser()#生成一个实例
cf.read("配置文件名") #读取配置文件
三、ConfigParser 基本操作
1.基本的读取配置文件
-read(filename) 直接读取.conf文件内容。
-sections() 得到所有的section,并以列表的形式返回。
['mongoDb', 'filePath']-options(section) 得到该section的所有option,并以列表的形式返回。
['username', 'password', 'database', 'tablename', 'mongodb_ip_port']-items(section) 得到该section的所有键值对,以列表的形式返回
[('username', 'dsg'), ('password', ''), ('database', 'promo'), ('tablename', 'trace'), ('mongodb_ip_port', '127.0.0.1:3717')]-get(section,option) 得到section中option的值,返回为string类型
123 <type 'str'>-getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。注意类型不对会报错。
123 <type 'int'>
2.基本的写入配置文件
-add_section(section) 添加一个新的section
conf.add_section('abcd')-set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
1 conf.set('abcd','haha','dsadasdasdada') #添加
2 conf.set('mongoDb','passWord','2222222')#修改注意:没有write()操作是不生效的。
conf.write(open('F:\\clientconf.conf','w')
四、具体使用示例
1.获取所有的sections
也就是将配置文件中所有“[ ]”读取到列表中:
sectionsList=conf.sections()
print 'sectionsList:',sectionsList
#输出结果为
['mongoDb', 'filePath']
2.获取指定sections的opention
即将配置文件某个section 内key 读取到列表中:
1 optionsList=conf.options('mongoDb')
2 print 'optionsList:',optionsList
3 #输出结果
4 ['username', 'password', 'database', 'tablename', 'mongodb_ip_port']
3.获取指定的sections的配置信息
获取指定节点下所有的键值对
mongoDb=conf.items('mongoDb')
print 'mongoDb=',mongoDb
#输出结果是
[('username', 'dsg'), ('password', ''), ('database', 'promo'), ('tablename', 'trace'), ('mongodb_ip_port', '127.0.01:3717')]
4.按照类型读取指定的sections的opention信息
print 'str=',conf.get('mongoDb','passWord')
print 'strType=',type(conf.get('mongoDb','passWord'))#
print 'int=',conf.getint('mongoDb','passWord')
print 'intType=',type(conf.getint('mongoDb','passWord'))#注意:类型不对,会报错误!
#输出结果:
2222222
<type 'str'>
2222222
<type 'int'>
5.添加opention和设置某个opention的值
conf.add_section('abcd')#添加section
conf.set('abcd','haha','dsadasdasdada')#添加opentions
conf.set('mongoDb','passWord','')#修改 opentions
conf.write(open('F:\\clientconf.conf','w'))#只有写入才有效
6.删除section或者opention
cf.remove_option('liuqing','int')
cf.remove_section('liuqing')
cf.write(open("test.conf", "w")
7.检查是否存在
has_sec = obj.has_section("mysql1") # false
print(has_sec)
解析配置文件ConfigParser模块的更多相关文章
- Python操作配置文件configparser模块
在实际的开发过程中,我们常有操作ini格式和conf格式配置文件的操作,Python为我们提供了configparser模块,方便我们对配置文件进行读写操作. config.ini配置文件内容如下: ...
- python 配置文件 ConfigParser模块
ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 [DEFAULT] Se ...
- [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]
[xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...
- python ConfigParser模块 配置文件解析
ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] da ...
- Python模块:配置文件解析器configparser
版权声明:本文为博主皮皮http://blog.csdn.net/pipisorry原创文章,未经博主同意不得转载. https://blog.csdn.net/pipisorry/article/d ...
- Python3 中 configparser 模块解析配置的用法详解
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...
- python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
- 第二十一天,pickle json xml shelve configparser模块
今日内容 1.pcikle 专用于python语言的序列化 2.json 是一种跨平台的数据格式 也属于序列化的一种方式 3.xml 可拓展标记语言 一种编写文档的语法 也支持跨平台 比较json而言 ...
- python3 之configparser 模块
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近[db]db_count = 31 = passwd2 = dat ...
随机推荐
- Amazon MWS 上传数据 (三) 提交请求
前面介绍了设置服务和构造请求,现在介绍提交请求. 上传数据,查询上传操作的工作状态,和处理上传操作返回的报告操作使用的Amazon API 分别为:SubmitFeed(),FeedSubmissio ...
- Android AndroidManifest 清单文件以及权限详解!【转】
转自:http://my.oschina.net/yuanxulong/blog/366753 每个Android应用都需要一个名为AndroidManifest.xml的程序清单文件,这个清单文件名 ...
- vim简单命令教程-firstblood
你想以最快的速度学习人类史上最好的文本编辑器VIM吗?你先得懂得如何在VIM幸存下来,然后一点一点地学习各种戏法. Vim the Six Billion Dollar editor Better, ...
- CodeForces 540B School Marks(思维)
B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Little Zu Chongzhi's Triangles
Little Zu Chongzhi's Triangles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 ...
- lua的string库与强大的模式匹配
lua原生解释器对字符串的处理能力是十分有限的,强大的字符串操作能力来自于string库.lua的string函数导出在string module中.在lua5.1,同一时候也作为string类型的成 ...
- Android上下左右滑动,显示底层布局
转载博客地址:http://www.cnblogs.com/flyme2012/p/4106308.html 闲着没事做了一个小东西.Android的上下左右滑动,显示底层布局.可以做类似于QQ消息列 ...
- Fix The thread xxx has exited with code 259 (0×103)
When run the test case in VS2013, you may encounter below problem After test case end, it will show ...
- FPGA开发(2)
1. 通常SPI通信的验证流程: 2. 对于主机而言,这里的FPGA为从机,而我们最关心SPI_CS,SPI_CSK,SPI_MISI这三个信号.SPI_CS为片选使能端,片选有效时FPGA才可以接受 ...
- read和onload jquery.val
$(document).load(); 当web页面以及其附带的资源文件,如CSS,Scripts,图片等,加载完毕后执行此方法.常用于检测页面(及其附带资源)是否加载完毕. $(document). ...