18

This is very simple.I am sure I am missing something silly.

fp = open(r'D:\UserManagement\invitationTemplate.html', 'rb')        
html = Template(fp.read())
fp.close()
html.safe_substitute(toFirstName='jibin',fromFirstName='Vishnu')
print html 

When i run this code in intepreter directly,I get the proper output. But when I run it from a file.I get <string.Template object at 0x012D33B0>.How do I convert from string.Template object to string.I have tried str(html).By the bye wasn't print statement supposed to do that(string conversion) ?

0

3 Answers 3

19

safe_substitute returns, as a string, the template with the substitutions made. This way, you can reuse the same template for multiple substitutions. So your code has to be

print html.safe_substitute(toFirstName='jibin',fromFirstName='Vishnu')
Sign up to request clarification or add additional context in comments.

Comments

5

According to the docs you should take the return Value of safe_substitute

fp = open(r'D:\UserManagement\invitationTemplate.html', 'rb')        
html = Template(fp.read())
fp.close()
result = html.safe_substitute(toFirstName='jibin',fromFirstName='Vishnu')
print result 

Comments

2

the result is returned by safe_substitute method:

result = html.safe_substitute(toFirstName='jibin',fromFirstName='Vishnu')
print result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.