发布时间:2019-09-10 08:48:45编辑:auto阅读(2182)
from string import Template
def main():
cart = []
cart.append(dict(item='coke',price=11,qty= 1))
cart.append(dict(item='cake',price=12,qty=6))
cart.append(dict(item='fish',price = 1,qty =4))
t = Template("$qty * $item = $price")
total = 0
print "Cart"
for data in cart:
print t.substitute(data)
total += data["price"]
print "Total: %s"%(total,)
if __name__ == "__main__":
main()
from string import Template
class MyTemplate(Template):
delimiter = '&'
def main():
cart = []
cart.append(dict(item='coke',price=11,qty= 1))
cart.append(dict(item='cake',price=12,qty=6))
cart.append(dict(item='fish',price = 1,qty =4))
t = MyTemplate("&qty * &item = &price")
total = 0
print "Cart"
for data in cart:
print t.substitute(data)
total += data["price"]
print "Total: %s"%(total,)
if __name__ == "__main__":
main()
Cart
1 * coke = 11
6 * cake = 12
4 * fish = 1
Total: 24
>>> t = Template(“$you owe me $$0.”)
>>> t.substitute(dict(you=’James’))
“James owe me $0.”
>>> t = Template(“The ${back}yard is far away.”)
>>> t.substitute(dict(back=’ship’))
“The shipyard is far away.”
上一篇: Python OpenCV 图片反色、调
下一篇: 用Python编写一个高效的端口扫描器
51287
50738
41336
38147
32617
29516
28366
23237
23203
21528
1602°
2336°
1936°
1876°
2204°
1921°
2604°
4374°
4224°
2996°