ConfigParse 作用是解析配置文件。

配置文件格式如下

[test1]
num1: 10
[test2]
num1: 35

配置文件两个概念section和option, 在这个例子中第一个section是

[test1]
num1: 10

其中test1是section name, 而num1是option的name。ConfigParser同样是根据section和option来解析配置文件。

使用ConfigParser

import ConfigParser
conf = ConfigParser.ConfigParser()

解析对应的配置文件:

提供了两个方法

-read()

-readfp()

这两个函数都是读取配置文件内容,不同之处是read参数是文件,而readfp的参数是handle。

conf.read("test.config")

conf.readfp(open("test.config"))

获取配置文件内容

-get(section,option)

-getinit(section, option)

-getfloat(section, option)

-getboolean(section, option)

这应该是最经常用到的函数,从配置文件中获取对应数值。get获取对应数值,后面三个函数在get的基础上进行了数值的转换。

conf.get('test1', 'num1')
conf.getint('test2', 'num1')

第一行返回的是字符串10,第二行返回的是数值35。其他两个函数同理。

需要注意的是getboolean(section,option)虽然返回True/False,在配置文件中可以为on/off True/False yes/no。方便了开关的配置。

其余的函数则是对section,option和item的显示和判断操作

比如has_section, 判断在配置文件中是否包含这个section

conf.has_section('test1')

类似函数has_option(section, option), 判断section中是否包含option

显示内容函数例

例如函数sections,显示所有的section

options, 显示一个section下所有的option

items,以(option,value)的形式显示section下所以的数值,返回列表。

>>> conf.items('test1')
[('num1', '10')]
>>> conf.sections()
['test1', 'test2']
>>> conf.options('test2')
['num1']
>>>

在ConfigParser中还有一类函数修改添加option,section然后写入到配置文件文档。

在我看来这是一个使用频率很低的函数。在一般程序中很少使用到。所以这个地方就不做讲解。

python - ConfigParser的更多相关文章

  1. python configparser模块

    来看一个好多软件的常见文档格式如下: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 Forward ...

  2. python ConfigParser配置读写

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号"[ ]"内包含的为section.section 下面为类似于key ...

  3. python ConfigParser、shutil、subprocess、ElementTree模块简解

    ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...

  4. [Python]ConfigParser解析配置文件

    近期发现非常多接口配置都硬编码在souce file中了,于是就看了下python怎么解析配置文件,重构下这一块. 这个应该是早就要作的... 配置文件: [mysqld] user = mysql ...

  5. Python configparser 读取指定节点内容失败

    # !/user/bin/python # -*- coding: utf-8 -*- import configparser # 生成一个config文件 config = configparser ...

  6. python configparser使用

    .ini文件由若干section(部分)组成, 而每一个section又由若干键值对组成. 以 example.ini为例: [DEFAULT] ServerAliveInterval = 45 Co ...

  7. Python Configparser模块读取、写入配置文件

    写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...

  8. Python ConfigParser的使用

    1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...

  9. python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法

    先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...

  10. 【转载】Python ConfigParser的使用

    1.基本的读取配置文件-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section ...

随机推荐

  1. 09_IO流

    1. IO(Input Output)流 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作数据分为两种: 字节流和字符流 流按类型分 ...

  2. AngularJs基础(一)

    使用 angularjs首先在页面的<html>里添加一个模块写法: <html lang="en"ng-app="myApp"> my ...

  3. 转载:Java面试笔试题大汇总

    本文来源于:http://blog.csdn.net/wulianghuan 1.面向对象的特征有哪些方面 1).抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关 ...

  4. HBase 分布式环境搭建

    一.前期环境 安装概览 IP Host Name Software Node 192.168.23.128 ae01 JDK 1.7, Zookeeper-3.4.5 HMaster 192.168. ...

  5. WPF学习笔记(3):Path绘制命令zz

    WPF的XAML提供了一系列功能强大.用法复杂的 mini-language 来描述可扩展应用程序标记语言 (XAML) 中的几何路径.如下所示: XAML <Canvas>   < ...

  6. BZOJ2082 : [Poi2010]Divine divisor

    将所有数分解质因数,那么第一问就是求指数的最大值,第二问就是$2^{指数最大的质数个数}-1$. 首先将$10^6$以内的质因数全部找到,那么剩下部分的因子$>10^6$,且只有3种情况: 1. ...

  7. mysql数据向Redis快速导入

    Redis协议 *<args><cr><lf> 参数个数 $<len><cr><lf> 第一个参数长度 <arg0> ...

  8. 关于jQuery的inArray 方法介绍

    例如: 代码如下: $.get('aaaaa.ashx',null,function(d){ // 假设d 返回 的值为 1,3,43,23,54,67 var arr = d.split(','); ...

  9. URAL 1117. Hierarchy(DP)

    题目链接 这破题,根本看不懂题意啊...题意:一棵中序遍历是1 2 3 4 5...的满二叉树,从a a+1 a+2 a+3 b,总共多少步.x到y的距离为中间有多少个点.a > b没注意2Y. ...

  10. 【BZOJ3732】 Network Kruskal+倍增lca

    Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_ ...