一.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模块的更多相关文章

  1. Python操作配置文件configparser模块

    在实际的开发过程中,我们常有操作ini格式和conf格式配置文件的操作,Python为我们提供了configparser模块,方便我们对配置文件进行读写操作. config.ini配置文件内容如下: ...

  2. python 配置文件 ConfigParser模块

    ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 [DEFAULT] Se ...

  3. [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]

    [xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...

  4. python ConfigParser模块 配置文件解析

    ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] da ...

  5. Python模块:配置文件解析器configparser

    版权声明:本文为博主皮皮http://blog.csdn.net/pipisorry原创文章,未经博主同意不得转载. https://blog.csdn.net/pipisorry/article/d ...

  6. Python3 中 configparser 模块解析配置的用法详解

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...

  7. python:实例化configparser模块读写配置文件

    之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...

  8. 第二十一天,pickle json xml shelve configparser模块

    今日内容 1.pcikle 专用于python语言的序列化 2.json 是一种跨平台的数据格式 也属于序列化的一种方式 3.xml 可拓展标记语言 一种编写文档的语法 也支持跨平台 比较json而言 ...

  9. python3 之configparser 模块

    configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近[db]db_count = 31 = passwd2 = dat ...

随机推荐

  1. css3实现的三种loading动画(转载)

    收藏了: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  2. JNDI(转载)

    转自:http://javacrazyer.iteye.com/blog/759485 原理:         在DataSource中事先建立多个数据库连接,保存在数据库连接池中.当程序访问数据库时 ...

  3. Oracle EBS-SQL (INV-4):检查负库存记录数.sql

    DEFINE DATE1="01/15/20** 23:59:59"      /*输入指定日期*/DEFINE CODE="%"                ...

  4. webservice axis2客户端设置代理方法(公司网络通过代理访问时)

    webservice axis2客户端设置代理方法(公司网络通过代理访问时)   UploadProcessInServiceStub stub = new UploadProcessInServic ...

  5. OpenStreetMap(OSM) for developers

    This article from: http://wiki.openstreetmap.org/wiki/Develop OpenStreetMap isn't just open data - i ...

  6. C4.5较ID3的改进

    1.ID3选择最大化Information Gain的属性进行划分   C4.5选择最大化Gain Ratio的属性进行划分 规避问题:ID3偏好将数据分为很多份的属性 解决:将划分后数据集的个数考虑 ...

  7. 关于ztree打开关闭所有节点,选中指定id节点

    var isOneByOneExpand=false;//是否递归展开 //展开节点 function expendNode(nodeId){ var node = treeObj.getNodeBy ...

  8. Yii的场景

    先上代码 class User extends CActiveRecord{    public function rules()    {        return array(          ...

  9. Unable to open ...\tools\capture\allegro.cfg for reading

    采用Capture CIS 当生成网表.误: Unable to open ...\tools\capture\allegro.cfg for reading. Please correct the ...

  10. CM_RESOURCE_LIST structure

    The CM_RESOURCE_LIST structure specifies all of the system hardware resources assigned to a device. ...