1
import string
s = '''\
Hi $name.
$contents
Have a good day
'''
t = string.Template(s)
contents = t.substitute(name='Mike', contents='How are you?')
print(contents)
Execution result of 1
Hi Mike.
How are you?
Have a good day
Of this Hi $name. $contents Have a good day If you put it in another text file and operate it, You can avoid accidentally editing the template.
For example Mail_template.txt in a directory called design Create a text file The contents of the text file Hi $name. $contents Have a good day And then You can write as follows.
2
import string
with open('design\mail_template.txt') as f:
t = string.Template(f.read())
contents = t.substitute(name='Mike', contents='How are you?')
print(contents)
Execution result of 2
Hi Mike.
How are you?
Have a good day
Recommended Posts