Python ConfigParser使用

来源:互联网 发布:怎么在淘宝上传图片 编辑:程序博客网 时间:2024/05/21 09:56

http://www.cnblogs.com/lijinrui/p/5619360.html
http://blog.csdn.net/gexiaobaohelloworld/article/details/7976944
http://blog.csdn.net/windone0109/article/details/10550383

1. 简介

ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。

[sec_a]  ; a commenta_key1 = 20  a_key2 = 10  [sec_b]  b_key1 = 121  b_key2 = b_value2  b_key3 = $r  b_key4 = 127.0.0.1  

其中, 中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容

模块安装:

sudo pip install configparser

主要方法:

  • 读取配置文件

    • read(filename) 直接读取ini文件内容
    • sections() 得到所有的section,并以列表的形式返回
    • options(section) 得到该section的所有option
    • items(section) 得到该section的所有键值对
    • get(section,option) 得到section中option的值,返回为string类型
    • getint(section,option) 得到section中option的值,返回为int类型
  • 写入配置文件

    • add_section(section) 添加一个新的section
    • set( section, option, value) 对section中的option进行设置需要调用write将内容写入配置文件。

ConfigParser 初始工作

使用ConfigParser 首选需要初始化实例,加载并读取配置文件:

import ConfigParsercfg = ConfigParser.ConfigParser()cfg.read('test.cfg') # 配置文件名

ConfigParser 常用方法

  1. 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:

    s = cfg.sections()print s

    将输出(以下将均以简介中配置文件为例):
    [‘sec_a’, ‘sec_b’]

  2. 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:

    opts = cfg.options('sec_a')print opts

    输出:
    [‘a_key1’, ‘a_key2’]

  3. 获取指定section 的配置信息。

    v = cfg.items('sec_a')

    输出:
    [(‘a_key1’, ‘20’), (‘a_key2’, ‘10’)]

  4. 按照类型读取指定section 的option 信息
    同样的还有getfloat、getboolean。

    #可以按照类型读取出来# 直接取 sec_a中值a_key1 = cfg.get('sec_a', 'a_key1')a_key2 = cfg.getint('sec_a', 'a_key2')# 直接取 sec_b中值b_key1 = cfg.getint('sec_b', 'b_key1')b_key2 = cfg.get('sec_b', 'b_key2')b_key3 = cfg.get('sec_b', 'b_key3')b_key4 = cfg.get('sec_b', 'b_key4')print 'a_key1', type(a_key1), a_key1 print 'a_key2', type(a_key2), a_key2print 'b_key1', type(b_key1), b_key1 print 'b_key2', type(b_key2), b_key2print 'b_key3', type(b_key3), b_key3 print 'b_key4', type(b_key4), b_key4# 将输出:a_key1 <type 'str'> 20a_key2 <type 'int'> 10b_key1 <type 'int'> 121b_key2 <type 'str'> b_value2b_key3 <type 'str'> $rb_key4 <type 'str'> 127.0.0.1
  5. 设置某个option 的值。(记得最后要写回)

    cfg.set('sec_a', 'a_key3', 'PSP')cfg.write(open('test.conf', 'w'))
  6. 添加一个section。(同样要写回)

    cfg.add_section('sec_c')cfg.set('sec_c', 'int', 15)cfg.set('sec_c', 'bool', True)cfg.set('sec_c', 'float', 3.1415)cfg.set('sec_c', 'baz', 'fun')cfg.write(open('test.conf', 'w'))
  7. 移除section 或者option 。(只要进行了修改就要写回的哦)

    cfg.remove_option('sec_a', 'a_key1')cfg.remove_section('sec_b')cfg.write(open('test.conf', 'w'))

最后附上示例中用到的完整代码:

import ConfigParsercfg = ConfigParser.ConfigParser()cfg.read('test.cfg')s = cfg.sections()print sopts = cfg.options('sec_a')print optsv = cfg.items('sec_a')print va_key1 = cfg.get('sec_a', 'a_key1')a_key2 = cfg.getint('sec_a', 'a_key2')b_key1 = cfg.getint('sec_b', 'b_key1')b_key2 = cfg.get('sec_b', 'b_key2')b_key3 = cfg.get('sec_b', 'b_key3')b_key4 = cfg.get('sec_b', 'b_key4')print 'a_key1', type(a_key1), a_key1 print 'a_key2', type(a_key2), a_key2print 'b_key1', type(b_key1), b_key1 print 'b_key2', type(b_key2), b_key2print 'b_key3', type(b_key3), b_key3 print 'b_key4', type(b_key4), b_key4cfg.set('sec_a', 'a_key3', 'PSP')cfg.add_section('sec_c')cfg.set('sec_c', 'int', 15)cfg.set('sec_c', 'bool', True)cfg.set('sec_c', 'float', 3.1415)cfg.set('sec_c', 'baz', 'fun')cfg.remove_option('sec_a', 'a_key1')cfg.remove_section('sec_b')cfg.write(open('test.conf', 'w'))

补充:

#!/usr/bin/env pythonimport ConfigParserimport sysconfig=ConfigParser.ConfigParser()config.add_section("book1")config.set("book1","title","hello world")config.set("book1","aut","log")config.write(open("f.txt","w"))