一.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. Delphi SysErrorMessage 函数和系统错误信息表

    在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示. 但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢? FormatMes ...

  2. Windows 10开发基础——文件、文件夹和库(一)

    原文:Windows 10开发基础--文件.文件夹和库(一) 主要内容: 1.枚举查询文件和文件夹 2.文本文件读写的三种方法——创建写入和读取文件 3.获得文件的属性 枚举查询文件和文件夹 先了解一 ...

  3. 在chart上加入一条指示线

    原文 http://hi.baidu.com/fuwei_bj/item/7e576410e970683db831801a <mx:AreaChart width="521" ...

  4. perl 公交车查询

    <pre name="code" class="cpp">decode_json 必须是unicode形式的字符,Dump不支持显示unicode形 ...

  5. 使用mod_cluster进行apache httpd server和jboss eap 6.1集群配置

    本文简单介绍,使用mod_cluster进行apache httpd server和jboss eap 6.1集群配置.本配置在windows上测试通过,linux下应该是一样的.可能要稍作调整.后面 ...

  6. 全国计算机等级考试二级教程-C语言程序设计_第13章_编译预处理和动态存储分配

    free(p);//释放内存 p = NULL;//软件工程规范,释放内存以后,指针应该赋值为空 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h ...

  7. IOS-时间与字符串互相转换

    有时会遇到这种问题,须要把时间和时间戳互相转换 比方把"这种格式 或者是把""转换成"2014-07-16 15:54:36" 首先来第一个: 当前时 ...

  8. EC读书笔记系列之10:条款16、17

    条款18 让接口容易被正确使用,不易被误用 记住: ★“促进正确使用”的办法包括接口的一致性,以及与内置类型的行为兼容 ★“阻止误用”的办法包括建立新类型.限制类型上的操作,束缚对象值,以及消除客户的 ...

  9. Mysql动态多条件查询

    动态多条件查询是一类经常遇到的问题. 在Mysql里面可以用语句简单的解决. SELECT * FROM product WHERE price = IF('{0}' = '', price, '{0 ...

  10. OC语法5——@property和@synthesize

    @property和@synthesize: 我们回想一下: 在OC中我们定义一个Student类需要两个文件Student.h 和 Student.m. Student.h(声明文件):定义成员变量 ...