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 Objects(implements 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
相关推荐
官方离线安装包,亲测可用
python的ConfigParser模块
python库。 资源全名:configparser-5.2.0.tar.gz
Python ConfigParser示例这是Python的ConfigParser模块的一些简单说明性代码,我试图弄清楚如何在配置文件中存储更复杂的数据结构时编写了这些代码。 除了常规内容(例如从配置文件中获取单个变量/值)之外,此代码...
主要介绍了Python使用ConfigParser模块操作配置文件的方法,结合实例形式分析了Python基于ConfigParser模块针对配置文件的创建、读取、写入、判断等相关操作技巧,需要的朋友可以参考下
主要介绍了Python内置模块ConfigParser实现配置读写功能的方法,涉及Python使用ConfigParser模块进行配置读、写、修改、删除等操作的相关实现技巧,需要的朋友可以参考下
这篇文章主要介绍了Python使用configparser库读取配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 背景: 在写接口自动化框架,配置数据库连接时,测试...
我们在操作 ini 配置文件的时候 可以使用 Python 的 configparser 库 具体使用方法如下: from configparser import ConfigParser # 初始化 cf = ConfigParser() # 加载文件 cf.read('ini.ini') # 读取 user 节点下...
主要介绍了Python3中configparser模块读写ini文件并解析配置的用法详解,需要的朋友可以参考下
主要介绍了Python configparser模块封装及构造配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
配置解析器 基于Python的ConfigParser的NodeJS模块。 它实现了一个基本的配置文件解析器。 该结构与Windows INI文件非常相似。安装npm install configparser 文献资料查看完整的文档例子写作有两种方法可以将配置...
主要介绍了Python configparser模块应用过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
python3.6,对其配置文件的读写进行了修改,目的是在设置值(会导致写文件)时,对原始文件中的注释进行保留。由于是对原工具中代码的修改,所以建议在使用此文件进行覆盖之前,先对原文件进行备份,防止在一些极端...
python configparser类获取.ini文件配置内容
Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件。因此在程序开始位置要导入该模块,注意区分是python2还是python3,python3有一些改动 import ConfigParser #python 2.x import configparser...