configparser python_Python2.7 ConfigParser

该模块用来解析Microsoft Windows INI文件,就是我们平常所说的ini文件。INI文件是一种按照特点方式排列的文本文件。每一个INI文件结构都非常类似,由若干段落(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,等号右边的就是关键字对应的值(value)。其一般形式如下:

[Section1]

KeyWord1 = Valuel

KeyWord2 = Value2

[Section2]

KeyWord3 = Value3

KeyWord4 = Value4

配置文件由section组成,每个section里面由name=value或者name:value组成,values中的空白符会被移除,在同一个section下的values可以包含该section下的其他values,以格式化字符串的形式表现,或者该values在DEFAULT section中定义过。额外的DEFAULT section可以提供values的初始化,以#开头的为注释。

该模块下有三个类:

ConfigParser.RawConfigParser([defaults[, dict_type[, allow_no_value]]])

这是基本的配置类,该类不支持魔术插入。方法如下:

RawConfigParser.defaults() 返回一个包含全部实例的字典

RawConfigParser.sections() 返回一个包含有效section的列表,DEFAULT不包含在该列表中

RawConfigParser.add_section(section) 增加一个section,如果section存在DuplicateSectionError会被触发。

RawConfigParser.has_section(section) 判断section是否在配置文件中存在

RawConfigParser.options(section) 返回section中可用的options 列表

RawConfigParser.has_option(section, option) 判断section中是否存在options

RawConfigParser.read(filenames) 读入被解析的配置文件

RawConfigParser.readfp(fp[, filename]) 读入并解析配置文件

RawConfigParser.get(section, option) 获取section中option的值

RawConfigParser.getint(section, option) 已整形返回option的值

RawConfigParser.getfloat(section, option) 同理上面,返回float

RawConfigParser.getboolean(section, option)

RawConfigParser.items(section) 以列表(name,value)的形式返回section中的每个值

RawConfigParser.set(section, option, value) 如果section存在,则设置该option和value,否则引起NoSectionError.

RawConfigParser.write(fileobject) 配置写入配置文件中

RawConfigParser.remove_option(section, option) 移除section中的option,如果section不存在,引起NoSectionError,移除后返回True,否则返回False

RawConfigParser.remove_section(section) 移除section,返回True/False

RawConfigParser.optionxform(option)

ConfigParser.ConfigParser([defaults[, dict_type[, allow_no_value]]])

该类是RawConfigParser的派生类。支持魔术插入,增加了get和items方法。

ConfigParser.get(section, option[, raw[, vars]]) 获取section中option的值,

ConfigParser.items(section[, raw[, vars]]) 返回一个由(name,value)组成的列表对

ConfigParser.SafeConfigParser([defaults[, dict_type[, allow_no_value]]])

该类是ConfigParser的派生类,支持更多的魔术插入。

SafeConfigParser.set(section, option, value) 如果section存在,则设置option的值。value必须是string。

生成ini配置文件

#!/usr/bin/python

import ConfigParser

conf=ConfigParser.RawConfigParser()

conf.add_section('section1')

conf.add_section('section2')

conf.set('section1','name1','guol')

conf.set('section1','name2','alex')

conf.set('section2','name3','polo')

conf.set('section2','name4','mark')

conffile=open('file.ini','wb')

conf.write(conffile)

结果如下:

解析ini配置文件

import ConfigParser

conf=ConfigParser.RawConfigParser()

conf.read('file.ini')

if conf.has_section('section2'):

print 'Exist section2'

if conf.has_option('section2','name3'):

print 'section2 has opetion name3, is value' + ' ' +conf.get('section2','name3')

结果如下:

魔术插入

import ConfigParser

conf1=ConfigParser.ConfigParser()

conf1.read('file.ini')

conf2=ConfigParser.RawConfigParser()

conf2.read('file.ini')

print 'Use ConfigParser()'

print '''conf1.get('section3','name3',0)'''

print conf1.get('section3','name3',0)

print '''conf1.get('section3','name3',1)'''

print conf1.get('section3','name3',1)

print '================================'

print 'Use RawConfigParser()'

print '''conf2.get('section3','name3')'''

print conf2.get('section3','name3')

结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值