这很简单。我相信我错过了一些愚蠢的东西。
fp = open(r'D:\UserManagement\invitationTemplate.html', 'rb')
html = Template(fp.read())
fp.close()
html.safe_substitute(toFirstName='jibin',fromFirstName='Vishnu')
print html
当我直接在解释器中运行这段代码时,可以得到正确的输出。但是,当我从文件中运行它时,会得到 <string.Template object at 0x012D33B0>
的输出。如何将 string.Template 对象转换为字符串?我尝试过使用 str(html)
,顺便问一句,print 语句不应该可以实现这个(字符串转换)吗?