2014/02/03

Python 3.3 Template strings



from string import Template
import sys

try:
    s = Template("$I love $WHO")
    print(s.substitute(I="I", WHO="god"))
    print(s.substitute(I="He", WHO="cat"))
    print(s.substitute(I=123, WHO=1))
    print(s.substitute(I=456, WHOO=1))
except KeyError:
    print(sys.exc_info())


當目標設定錯名稱,會噴出KeyError的例外



參考資料:
http://docs.python.org/3/library/string.html#template-strings