标准库 2.x 中提供的古老 ConfigParser 模块在 Python 3.2 中进行了重大更新。 这是这些更改的向后移植,以便它们可以直接在 Python 2.6 - 3.5 中使用。
要在 Python 2 和 Python 3 上使用 configparser 反向移植而不是内置版本,只需将其显式导入为反向移植:
from backports import configparser
如果您想在 Python 2 上使用向后移植并在 Python 3 上使用内置版本,请改用该调用:
import configparser
For detailed documentation consult the vanilla version at http://docs.python.org/3/library/configparser.html.
configparserWhereas almost completely compatible with its older brother, configparser sports a bunch of interesting new features:
full mapping protocol access (more info):
>>> parser = ConfigParser()
>>> parser.read_string("""
[DEFAULT]
location = upper left
visible = yes
editable = no
color = blue
[main]
title = Main Menu
color = green
[options]
title = Options
""")
>>> parser['main']['color']
'green'
>>> parser['main']['editable']
'no'
>>> section = parser['options']
>>> section['title']
'Options'
>>> section['title'] = 'Options (editable: %(editable)s)'
>>> section['title']
'Options (editable: no)'
there's now one default ConfigParser class, which basically is the old SafeConfigParser with a bunch of tweaks which make it more predictable for users. Don't need interpolation? Simply use ConfigParser(interpolation=None), no need to use a distinct RawConfigParser anymore.
the parser is highly customizable upon instantiation supporting things like changing option delimiters, comment characters, the name of the DEFAULT section, the interpolation syntax, etc.
you can easily create your own interpolation syntax but there are two powerful implementations built-in (more info):
%(string-like)s syntax (called BasicInterpolation)${buildout:like} syntax (called ExtendedInterpolation)fallback values may be specified in getters (more info):
>>> config.get('closet', 'monster',
... fallback='No such things as monsters')
'No such things as monsters'
ConfigParser objects can now read data directly from strings and from dictionaries. That means importing configuration from JSON or specifying default values for the whole configuration (multiple sections) is now a single line of code. Same goes for copying data from another ConfigParser instance, thanks to its mapping protocol support.
many smaller tweaks, updates and fixes
configparser 来自 Python 3,因此它可以很好地处理 Unicode。 库一般会在内部数据存储和读/写文件方面进行清理。 因此,与旧的 ConfigParser 存在一些不兼容性。 然而,迁移所需的工作是非常值得的,因为它显示了将项目迁移到 Python 3 期间可能出现的问题。
该设计假设尽可能使用 Unicode 字符串 [1]。 这使您可以确定配置对象中存储的内容是文本。 读取配置后,应用程序的其余部分就不必处理编码问题。 您拥有的只是文本 [2]。 您应该明确声明编码的唯一两个阶段是当您从外部源(例如文件)读取或写回时。
此向后移植旨在与 Python 3.2+ 中的普通版本保持 100% 兼容性。 为了帮助维护您想要和期望的版本,在以下位置使用版本控制方案:
For example, 3.5.2 is the third backport release of the configparser library as seen in Python 3.5. Note that 3.5.2 does NOT necessarily mean this backport version is based on the standard library of Python 3.5.2.
One exception from the 100% compatibility principle is that bugs fixed before releasing another minor Python 3 bugfix version will be included in the backport releases done in the mean time.
# -*- coding: utf-8 -*-
from ConfigParser import ConfigParser
if __name__ == '__main__':
configFile = 'certNotic.ini'
cp = ConfigParser()
cp.read(configFile)
########读配置########
#读取所有section组成的列表,如返回['section_a', 'section_b']这样子
print(cp.sections())
#读取某个section中所有的option,如返回['a_key1', 'a_key2']这样子
print(cp.options('Alias'))
#读取某个section中所有的option, value,如返回[('a_key1','a_value1'),...]这样子
print(cp.items('Alias'))
#读取某个value了
print(cp.get('Alias','pophost'))
#获取Dict
items = cp.items('Alias')
commandDict = {}
for key, value in items:
commandDict[key] = value
print(commandDict)
########写配置########
#read某个文件,相当于是把这个文件的内容加载进了内存里
#然后可以进行以下的一些操作,把它修改掉
#增加一条新纪录,这里键可以写已存在的,那就是更新现有的value值
cp.set('Alias','a_key3','a_value3')
#增加一条新section
cp.add_section('section_c')
#最后,把内存中改完的东西固化到一个文件中去,值得注意的是参数不是路径而是一个文件对象!
cp.write(open(configFile,"w"))