The ConfigParser module in the standard library already does this:
import ConfigParser
cfg = ConfigParser.ConfigParser()
cfg.readfp(open('myconfig.ini'))
print cfg.get('system', 'database')
----
The configuration file consists of sections, led by a "[section]" header and followed by "name: value" entries, with continuations in the style of RFC 822; "name=value" is also accepted. Note that leading whitespace is removed from values. The optional values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization and retrieval. Lines beginning with "#" or ";" are ignored and may be used to provide comments.
For example:
[My Section]
foodir: %(dir)s/whatever
dir=frob
would resolve the "%(dir)s" to the value of "dir" ("frob" in this case). All reference expansions are done on demand.
Default values can be specified by passing them into the ConfigParser constructor as a dictionary. Additional defaults may be passed into the get() method which will override all others.
---------------------------
Python 本身没有数组这个说法, 有的就是list和tuple, list就具有其他语言中的数组特性.
至于list和tuple的区别,在于list可以在运行时修改内容和大小,tuple在首次创建和赋值后, 不可以再次修改内部的内容
不过python 有提供一个array模块,用于提供基本数字,字符类型的数组.用于容纳字符号,整型,浮点等基本类型.
import array
#建立一个整数数组,初始内容是1,2,3,4,5
array.array('l', [1, 2, 3, 4, 5])
这种模块主要用于二进制上的缓冲区,流的操作.
Monday, 14 January 2008
Python reads configuration file
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment