解析配置文件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 ...
随机推荐
- c# 中的 Trim
1. 让用户输入字符串 并且判断是否是 'yes'(无关大小写) Console.WriteLine("input a string"); string userResponse ...
- rdlc部署zt
原文:rdlc部署zt 偶然间遇到“ 未能加载文件或程序集microsoft.reportviewer.winforms ……”的一个错误,以前web是遇到过,没想到winform部署也会遇到.找了半 ...
- [置顶] 通过实例学习Struts2 (1)
前言 一直用Struts1 , 现在新的项目要转向Struts2了, 先研究学习一下,做点技术储备. 我一直认为计算机软件行业是一个实践性非常强的行业,书读了多少都不管用, 一定要卷起袖子,亲自动手, ...
- AndroidUI 布局动画-为列表添加布局动画效果
新建一个Android project ,使MainActivity 继承自 ListActivity: public class MainActivity extends ListActivity ...
- php 多维数组 arrayList array()
<pre name="code" class="php">$params=array( "tid"=>"3&qu ...
- ostream类的公有成员函数
1 flush 2 operator<< 3 put 4 seekp 5 tellp 6 write 1 flush 刷新输出流 2 operator<< 插入运算符 3 pu ...
- bind新发现
function foo(a,b){ this.val = a+b; } var bar = foo.bind(null, 'p1'); var baz = new bar('p2'); consol ...
- mysql数据库学习(二)--表操作
一.表操作 以下内容都是自己学习的时候看过的一些知识,作为笔记记录一下吧,大部分都是所看文章的内容. 1.创建表 前面的基础篇笔记是相当于搭建了一个方便管理的文件夹树根,下面要学习的是一些关于表的知识 ...
- mysql中多个字段共同确定唯一性
create table tbl_table ( id integer not null auto_increment, fname varchar(255), lname varchar(255), ...
- 在自定义的js验证规则中调用magento的VarienForm方法验证表单
js部分<script type="text/javascript"> //<![CDATA[ var loginForm = new VarienForm('l ...