今天看drive into python 时,发现了字典格式化(DictFormatting)这个概念。
>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"} >>> "%(pwd)s" % params 1 'secret' >>> "%(pwd)s is not a good password for %(uid)s" % params 2 'secret is not a good password for sa' >>> "%(database)s of mind, %(database)s of body" % params 3 'master of mind, master of body'
然后回到工作中,我们使用的sql 插入变量:
db().get('SELECT * FROM test WHERE id = %(id)s', id=id)
现在,我还是回忆下python中格式化字符串的方法:
'{}'.format('chenguo') '{cg}'.format(cg=chenguo)
drive into python (DictFormatting): http://www.diveintopython.net/html_processing/dictionary_based_string_formatting.html