1. ConfigParser
format.conf
1 [DEFAULT] 2 conn_str = %(dbn)s://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s 3 dbn = mysql 4 user = root 5 host = localhost 6 port = 3306 7 8 [db1] 9 user = aaa 10 pw = ppp 11 db = example 12 13 [db2] 14 host = 172.16.88.1 15 pw = www 16 db = example
readformatini.py
1 import ConfigParser 2 3 conf = ConfigParser.ConfigParser() 4 conf.read('format.conf') 5 print conf.get('db1', 'conn_str') # mysql://aaa.ppp@localhost:3306/example 6 print conf.get('db2', 'conn_str') # mysql://root:www@172.16.88.1:3306/example
get(section, option[, raw[, vars]]) 的查找规则如下:
1)如果找不到节点名,就抛出 NoSectionError。
2)如果给定的配置项出现在 get() 方法的 vars 参数中,则返回 vars 参数中的值。
3)如果在指定的节点总含有给定的配置项,则返回其值。
4)如果在 [DEFAULT] 中有指定的配置项,则返回其值。
5)如果在构造函数的 default 参数中有指定的配置项,则返回其值。
6)抛出 NoOptionError。
2. 创建大文件的技巧
1 f = open('large.csv', 'wb') 2 f.seek(1073741824-1) # 创建大文件的技巧 3 f.write('