13.10 读��置文件¶

问题¶

怎样读�普通.ini格�的�置文件?

解决方案¶

configparser 模�能被用�读��置文件。例如,�设你有如下的�置文件:

; config.ini
; Sample configuration file

[installation]
library=%(prefix)s/lib
include=%(prefix)s/include
bin=%(prefix)s/bin
prefix=/usr/local

# Setting related to debug configuration
[debug]
log_errors=true
show_warnings=False

[server]
port: 8080
nworkers: 32
pid-file=/tmp/spam.pid
root=/www/root
signature:
    =================================
    Brought to you by the Python Cookbook
    =================================

下�是一个读�和��其中值的例�:

>>> from configparser import ConfigParser
>>> cfg = ConfigParser()
>>> cfg.read('config.ini')
['config.ini']
>>> cfg.sections()
['installation', 'debug', 'server']
>>> cfg.get('installation','library')
'/usr/local/lib'
>>> cfg.getboolean('debug','log_errors')

True
>>> cfg.getint('server','port')
8080
>>> cfg.getint('server','nworkers')
32
>>> print(cfg.get('server','signature'))

\=================================
Brought to you by the Python Cookbook
\=================================
>>>

如果有需�,你还能修改�置并使用 cfg.write() 方法将其写回到文件中。例如:

>>> cfg.set('server','port','9000')
>>> cfg.set('debug','log_errors','False')
>>> import sys
>>> cfg.write(sys.stdout)
[installation]
library = %(prefix)s/lib
include = %(prefix)s/include
bin = %(prefix)s/bin
prefix = /usr/local

[debug]
log_errors = False
show_warnings = False

[server]
port = 9000
nworkers = 32
pid-file = /tmp/spam.pid
root = /www/root
signature =
          =================================
          Brought to you by the Python Cookbook
          =================================
>>>

讨论¶

�置文件作为一��读性很好的格�,�常适用于存储程�中的�置数�。 在�个�置文件中,�置数�会被分组(比如例�中的“installation�� “debug� 和 “server�)。 �个分组在其中指定对应的�个��值。

对于�实现�样功能的�置文件和Python�文件是有很大的��的。 首先,�置文件的语法�更自由些,下�的赋值语�是等效的:

prefix=/usr/local
prefix: /usr/local

�置文件中的�字是�区分大�写的。例如:

>>> cfg.get('installation','PREFIX')
'/usr/local'
>>> cfg.get('installation','prefix')
'/usr/local'
>>>

在解�值的时候,getboolean() 方法查找任何�行的值。例如下�都是等价的:

log_errors = true
log_errors = TRUE
log_errors = Yes
log_errors = 1

或许�置文件和Python代�最大的��在于,它并�是从上而下的顺�执行。 文件是安装一个整体被读�的。如果碰到了��替�,它实际上已�被替�完�了。 例如,在下�这个�置中,prefix ��在使用它的��之�或之�定义都是�以的:

[installation]
library=%(prefix)s/lib
include=%(prefix)s/include
bin=%(prefix)s/bin
prefix=/usr/local

ConfigParser 有个容易被忽视的特性是它能一次读�多个�置文件然��并�一个�置。 例如,�设一个用户�下�这样构造了他们的�置文件:

; ~/.config.ini
[installation]
prefix=/Users/beazley/test

[debug]
log_errors=False

读�这个文件,它就能跟之�的�置�并起�。如:

>>> # Previously read configuration
>>> cfg.get('installation', 'prefix')
'/usr/local'

>>> # Merge in user-specific configuration
>>> import os
>>> cfg.read(os.path.expanduser('~/.config.ini'))
['/Users/beazley/.config.ini']

>>> cfg.get('installation', 'prefix')
'/Users/beazley/test'
>>> cfg.get('installation', 'library')
'/Users/beazley/test/lib'
>>> cfg.getboolean('debug', 'log_errors')
False
>>>

仔细观察下 prefix ��是怎样覆盖其他相关��的,比如 library 的设定值。 产生这�结果的原因是��的改写采�的是��制人策略,以最�一个为准。 你�以�下�这样�试验:

>>> cfg.get('installation','library')
'/Users/beazley/test/lib'
>>> cfg.set('installation','prefix','/tmp/dir')
>>> cfg.get('installation','library')
'/tmp/dir/lib'
>>>

最�还有很��一点�注�的是Python并�能支�.ini文件在其他程�(比如windows应用程�)中的所有特性。 确�你已��阅了configparser文档中的语法详情以�支�特性。