新学使用yaml作为程序配置文件;感觉很好 -- 易读,易写,易解析,易用。
例如:# Yaml格式配置文件
host: dbhost
user: dbuser
password: "dbpwd"
database: thetestdb
sqlstring_summaryDaily: 
  select [date], charge, monthsummary
  from ds_collect
  where code=$code
  order by [date] descthreshold_values:
  "099023":
    name: name of the custmoer
    daily: 600.00
    monthSummary: 2500.00  "000000":
    name: 2nd customer's name
    daily: 800.00
    monthSummary: 3000.00# 以下为Python脚本代码 ##############################!python
# -*- coding:GBK -*-import yaml
import pymssql
import stringconfig = yaml.load(file('Config.yaml', 'r'))
conn = pymssql.connect(host=config['host'],
        user=config['user'],
        password=config['password'],
        database=config['database'])
conn.row_factory=str
curr = conn.cursor()pageTmpl = "$name .. <table><tr><td>Date</td><td>Daily</td><td>MnSummary</td></tr>$trs</table>" # 此处具体内容简化trFmt = "<tr><td>%s</td><td>%s</td><td>%s</td></tr>"for code in config['threshold_values']:
    tvalues = config['threshold_values'][code]
    dgate , mgate = float(tvalues['daily']) , float(tvalues['monthSummary'])
    trs , atrs = [] , ""
    
    curr.execute(string.Template(config['sqlstring_summaryDaily']).safe_substitute(dict(code=code)))
    for (date,dsum, msum,) in curr.fetchall():
        trs.append(trFmt % (
            date.strftime("%d, %b"), 
            dsum, 
            msum))    open("%s.html" % code,"w").writelines(pageTmpl.safe_substitute(dict(
        name = tvalues['name'], trs = "\n".join(trs), 
        )))curr.close()
conn.close()
>> 可以很方便地生成客户报表,并按需要修改配置文件..
但是如果在配置文件中存在中文(例如 ... name: 客户名称 ...)将遇到报错:
yaml.reader.ReaderError: 'utf8' codec can't decode byte #xb4: unexpected code byte ...
如何解决呢?