码迷,mamicode.com
首页 > 编程语言 > 详细

python DictFormatting

时间:2015-09-20 17:36:36      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

今天看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

 

python DictFormatting

标签:

原文地址:http://www.cnblogs.com/ChaserChen/p/4823763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!