template

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

template
Django HTML Template # 2
Django Template notes
Django HTML template
Django Template Tips
python argparse template
[Python] Tkinter template
PyTorch learning template
Competitive Pro Template (Python)
[Details] Template Method pattern
Design Pattern #Template Method
Template customization with PyDev
Python data analysis template
Jinja2 | Python template engine
Collage template automatic generation
python unit test template
Python template engine empy
Flask HTML template settings