`
rockkyle
  • 浏览: 10407 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

python_ConfigParser

阅读更多

Python ConfigParser(python3中是configparser

包含3个object

RawConfigParser Objects

有如下方法:

RawConfigParser.defaults() 

RawConfigParser.add_section(section)  

Add a section named section to the instance

RawConfigParser.sections()  

Return a list of the sections available

RawConfigParser.remove_section(section)  

Remove the specified section from the configuration

Example:

>>> import ConfigParser

>>> config=ConfigParser.RawConfigParser()

>>> config.defaults()

{}

>>> config.add_section('section1')

>>> config.add_section('section2')

>>> config.sections()

['section2', 'section1']

>>> config.remove_section('section2')

True

>>> config.sections()

['section1']

RawConfigParser.set(section, option, value) 

If the given section exists, set the given option to the specified value 

RawConfigParser.get(section, option)  

Get an option value for the named section.

RawConfigParser.getint(section, option)  

A convenience method which coerces the option in the specified section to an integer.

RawConfigParser.getfloat(section, option)  

A convenience method which coerces the option in the specified section to a floating point number.

RawConfigParser.getboolean(section, option) 

the accepted values for the option are "1", "yes", "true", and "on", which cause this method to return True, and "0", "no", "false", and "off", which cause it to return False.

RawConfigParser.items(section)  

Return a list of (name, value) pairs for each option in the given section

RawConfigParser.remove_option(section, option) 

Remove the specified option from the specified section

RawConfigParser.write(fileobject) 

Write a representation of the configuration to the specified file object

RawConfigParser.read(filenames)

Read and parse a list of filenames, returning a list of filenames which were successfully parsed.

Example

>>> config.set('section1','a_int','1')

>>> config.set('section1','a_float','1.1')

>>> config.set('section1','a_bool','true')

>>> config.set('section1','name','kyle')

>>> config.getint('section1','a_int')

1

>>> config.getfloat('section1','a_float')

1.1000000000000001

>>> config.getboolean('section1','a_bool')

True

>>>

>>> config.get('section1','name')

'kyle'

>>> f=open('text.txt','wb')

>>> config.write(f)

>>> f.close()

>>> config.read('text.txt')

['text.txt']

>>> for item in config.items('section1'):

...     print item

... 

('a_int', '1')

('a_bool', 'true')

('name', 'kyle')

('a_float', '1.1')

>>> config.remove_option('section1','a_int')

True

>>> for item in config.items('section1'):

...     print item

... 

('a_bool', 'true')

('a_float', '1.1')

('name', 'kyle')

 

ConfigParser Objects(extends some methods of the RawConfigParser interface):

ConfigParser.get(section, option[, raw[, vars]])

使用方法基本与RawConfigParser.get(section, option)相同,区别如下:

>>> config=ConfigParser.ConfigParser()

>>> config.read('text.txt')

['text.txt']

>>> config.set('section1','tt','%(a_int)s is not equal %(a_float)s')

>>> config.get('section1','tt',1)

'%(a_int)s is not equal %(a_float)s'  

>>> config.get('section1','tt',0)

'1 is not equal 1.1'

ConfigParser.items(section[, raw[, vars]])

RawConfigParser.items(section)中的区别 和get方法一样

SafeConfigParser Objectsimplements the same extended interface as ConfigParser):

SafeConfigParser.set(section, option, value)

Example:

>>> config=ConfigParser.SafeConfigParser({'a_int':'3'})

>>> config.read('text.txt')

['text.txt']

>>> print config.get('section1','a_int')

1

>>> config.remove_option('section1','a_int')

True

>>> print config.get('section1','a_int')

3

>>> 

参考:http://docs.python.org/2/library/configparser.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics