发布时间:2019-09-10 08:48:45编辑:auto阅读(1950)
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编写一个高效的端口扫描器
48802
47840
38614
35800
30223
26971
26009
20842
20626
19004
411°
491°
520°
527°
514°
496°
566°
629°
747°
787°