Skip to content

Commit cc5a9a4

Browse files
committed
13.10小节完成
1 parent 8e53a65 commit cc5a9a4

File tree

1 file changed

+133
-139
lines changed

1 file changed

+133
-139
lines changed

source/c13/p10_read_configuration_files.rst

Lines changed: 133 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -5,140 +5,136 @@
55
----------
66
问题
77
----------
8-
You want to read configuration files written in the common .ini configuration file
9-
format.
8+
怎样读取普通.ini格式的配置文件?
109

1110
|
1211
1312
----------
1413
解决方案
1514
----------
16-
The configparser module can be used to read configuration files. For example, suppose
17-
you have this configuration file:
18-
19-
; config.ini
20-
; Sample configuration file
21-
22-
[installation]
23-
library=%(prefix)s/lib
24-
include=%(prefix)s/include
25-
bin=%(prefix)s/bin
26-
prefix=/usr/local
27-
28-
# Setting related to debug configuration
29-
[debug]
30-
log_errors=true
31-
show_warnings=False
32-
33-
[server]
34-
port: 8080
35-
nworkers: 32
36-
pid-file=/tmp/spam.pid
37-
root=/www/root
38-
signature:
39-
=================================
15+
``configparser`` 模块能被用来读取配置文件。例如,假设你有如下的配置文件:
16+
17+
; config.ini
18+
; Sample configuration file
19+
20+
[installation]
21+
library=%(prefix)s/lib
22+
include=%(prefix)s/include
23+
bin=%(prefix)s/bin
24+
prefix=/usr/local
25+
26+
# Setting related to debug configuration
27+
[debug]
28+
log_errors=true
29+
show_warnings=False
30+
31+
[server]
32+
port: 8080
33+
nworkers: 32
34+
pid-file=/tmp/spam.pid
35+
root=/www/root
36+
signature:
37+
=================================
38+
Brought to you by the Python Cookbook
39+
=================================
40+
41+
下面是一个读取和提取其中值的例子:
42+
43+
.. code-block:: python
44+
45+
>>> from configparser import ConfigParser
46+
>>> cfg = ConfigParser()
47+
>>> cfg.read('config.ini')
48+
['config.ini']
49+
>>> cfg.sections()
50+
['installation', 'debug', 'server']
51+
>>> cfg.get('installation','library')
52+
'/usr/local/lib'
53+
>>> cfg.getboolean('debug','log_errors')
54+
55+
True
56+
>>> cfg.getint('server','port')
57+
8080
58+
>>> cfg.getint('server','nworkers')
59+
32
60+
>>> print(cfg.get('server','signature'))
61+
62+
\=================================
4063
Brought to you by the Python Cookbook
41-
=================================
42-
43-
Here is an example of how to read it and extract values:
44-
45-
>>> from configparser import ConfigParser
46-
>>> cfg = ConfigParser()
47-
>>> cfg.read('config.ini')
48-
['config.ini']
49-
>>> cfg.sections()
50-
['installation', 'debug', 'server']
51-
>>> cfg.get('installation','library')
52-
'/usr/local/lib'
53-
>>> cfg.getboolean('debug','log_errors')
54-
55-
True
56-
>>> cfg.getint('server','port')
57-
8080
58-
>>> cfg.getint('server','nworkers')
59-
32
60-
>>> print(cfg.get('server','signature'))
61-
62-
\=================================
63-
Brought to you by the Python Cookbook
64-
\=================================
65-
>>>
66-
67-
If desired, you can also modify the configuration and write it back to a file using the
68-
cfg.write() method. For example:
69-
70-
>>> cfg.set('server','port','9000')
71-
>>> cfg.set('debug','log_errors','False')
72-
>>> import sys
73-
>>> cfg.write(sys.stdout)
74-
[installation]
75-
library = %(prefix)s/lib
76-
include = %(prefix)s/include
77-
bin = %(prefix)s/bin
78-
prefix = /usr/local
79-
80-
[debug]
81-
log_errors = False
82-
show_warnings = False
83-
84-
[server]
85-
port = 9000
86-
nworkers = 32
87-
pid-file = /tmp/spam.pid
88-
root = /www/root
89-
signature =
90-
=================================
91-
Brought to you by the Python Cookbook
92-
=================================
93-
>>>
64+
\=================================
65+
>>>
66+
67+
如果有需要,你还能修改配置并使用 ``cfg.write()`` 方法将其写回到文件中。例如:
68+
69+
.. code-block:: python
70+
71+
>>> cfg.set('server','port','9000')
72+
>>> cfg.set('debug','log_errors','False')
73+
>>> import sys
74+
>>> cfg.write(sys.stdout)
75+
[installation]
76+
library = %(prefix)s/lib
77+
include = %(prefix)s/include
78+
bin = %(prefix)s/bin
79+
prefix = /usr/local
80+
81+
[debug]
82+
log_errors = False
83+
show_warnings = False
84+
85+
[server]
86+
port = 9000
87+
nworkers = 32
88+
pid-file = /tmp/spam.pid
89+
root = /www/root
90+
signature =
91+
=================================
92+
Brought to you by the Python Cookbook
93+
=================================
94+
>>>
9495
9596
|
9697
9798
----------
9899
讨论
99100
----------
100-
Configuration files are well suited as a human-readable format for specifying configu‐
101-
ration data to your program. Within each config file, values are grouped into different
102-
sections (e.g., “installation,” “debug,” and “server,” in the example). Each section then
103-
specifies values for various variables in that section.
104-
There are several notable differences between a config file and using a Python source
105-
file for the same purpose. First, the syntax is much more permissive and “sloppy.” For
106-
example, both of these assignments are equivalent:
101+
配置文件作为一种可读性很好的格式,非常适用于存储程序中的配置数据。
102+
在每个配置文件中,配置数据会被分组(比如例子中的“installation”、 “debug” 和 “server”)。
103+
每个分组在其中指定对应的各个变量值。
107104

108-
prefix=/usr/local
109-
prefix: /usr/local
105+
对于可实现同样功能的配置文件和Python源文件是有很大的不同的。
106+
首先,配置文件的语法要更自由些,下面的赋值语句是等效的:
110107

111-
The names used in a config file are also assumed to be case-insensitive. For example:
108+
prefix=/usr/local
109+
prefix: /usr/local
110+
111+
配置文件中的名字是不区分大小写的。例如:
112112

113-
>>> cfg.get('installation','PREFIX')
114-
'/usr/local'
115-
>>> cfg.get('installation','prefix')
116-
'/usr/local'
117-
>>>
113+
>>> cfg.get('installation','PREFIX')
114+
'/usr/local'
115+
>>> cfg.get('installation','prefix')
116+
'/usr/local'
117+
>>>
118118

119-
When parsing values, methods such as getboolean() look for any reasonable value.
120-
For example, these are all equivalent:
119+
在解析值的时候,``getboolean()`` 方法查找任何可行的值。例如下面都是等价的:
121120

122121
log_errors = true
123122
log_errors = TRUE
124123
log_errors = Yes
125124
log_errors = 1
126125

127-
Perhaps the most significant difference between a config file and Python code is that,
128-
unlike scripts, configuration files are not executed in a top-down manner. Instead, the
129-
file is read in its entirety. If variable substitutions are made, they are done after the fact.
130-
For example, in this part of the config file, it doesn’t matter that the prefix variable is
131-
assigned after other variables that happen to use it:
126+
或许配置文件和Python代码最大的不同在于,它并不是从上而下的顺序执行。
127+
文件是安装一个整体被读取的。如果碰到了变量替换,它实际上已经被替换完成了。
128+
例如,在下面这个配置中,``prefix`` 变量在使用它的变量之前后之后定义都是可以的:
132129

133130
[installation]
134131
library=%(prefix)s/lib
135132
include=%(prefix)s/include
136133
bin=%(prefix)s/bin
137134
prefix=/usr/local
138135

139-
An easily overlooked feature of ConfigParser is that it can read multiple configuration
140-
files together and merge their results into a single configuration. For example, suppose
141-
a user made their own configuration file that looked like this:
136+
``ConfigParser`` 有个容易被忽视的特性是它能一次读取多个配置文件然后合并成一个配置。
137+
例如,假设一个用户像下面这样构造了他们的配置文件:
142138

143139
; ~/.config.ini
144140
[installation]
@@ -147,38 +143,36 @@ a user made their own configuration file that looked like this:
147143
[debug]
148144
log_errors=False
149145

150-
This file can be merged with the previous configuration by reading it separately. For
151-
example:
152-
153-
>>> # Previously read configuration
154-
>>> cfg.get('installation', 'prefix')
155-
'/usr/local'
156-
157-
>>> # Merge in user-specific configuration
158-
>>> import os
159-
>>> cfg.read(os.path.expanduser('~/.config.ini'))
160-
['/Users/beazley/.config.ini']
161-
162-
>>> cfg.get('installation', 'prefix')
163-
'/Users/beazley/test'
164-
>>> cfg.get('installation', 'library')
165-
'/Users/beazley/test/lib'
166-
>>> cfg.getboolean('debug', 'log_errors')
167-
False
168-
>>>
169-
170-
Observe how the override of the prefix variable affects other related variables, such as
171-
the setting of library. This works because variable interpolation is performed as late
172-
as possible. You can see this by trying the following experiment:
173-
174-
>>> cfg.get('installation','library')
175-
'/Users/beazley/test/lib'
176-
>>> cfg.set('installation','prefix','/tmp/dir')
177-
>>> cfg.get('installation','library')
178-
'/tmp/dir/lib'
179-
>>>
180-
181-
Finally, it’s important to note that Python does not support the full range of features you
182-
might find in an .ini file used by other programs (e.g., applications on Windows). Make
183-
sure you consult the configparser documentation for the finer details of the syntax
184-
and supported features.
146+
读取这个文件,它就能跟之前的配置合并起来。如:
147+
148+
>>> # Previously read configuration
149+
>>> cfg.get('installation', 'prefix')
150+
'/usr/local'
151+
152+
>>> # Merge in user-specific configuration
153+
>>> import os
154+
>>> cfg.read(os.path.expanduser('~/.config.ini'))
155+
['/Users/beazley/.config.ini']
156+
157+
>>> cfg.get('installation', 'prefix')
158+
'/Users/beazley/test'
159+
>>> cfg.get('installation', 'library')
160+
'/Users/beazley/test/lib'
161+
>>> cfg.getboolean('debug', 'log_errors')
162+
False
163+
>>>
164+
165+
仔细观察下 ``prefix`` 变量是怎样覆盖其他相关变量的,比如 ``library`` 的设定值。
166+
产生这种结果的原因是变量的改写采取的是后发制人策略,以最后一个为准。
167+
你可以像下面这样做试验:
168+
169+
>>> cfg.get('installation','library')
170+
'/Users/beazley/test/lib'
171+
>>> cfg.set('installation','prefix','/tmp/dir')
172+
>>> cfg.get('installation','library')
173+
'/tmp/dir/lib'
174+
>>>
175+
176+
最后还有很重要一点哟啊注意的是Python并不能支持.ini文件在其他程序(比如windows应用程序)中的所有特性。
177+
确保你已经参阅了configparser文档中的语法详情以及支持特性。
178+

0 commit comments

Comments
 (0)