dagg
11/6/2018 - 3:04 PM

Python Templates using external file

Python Templates using external file

'''
Let's suppose the external file `template.txt` contains the text:
`Hello $name, how are you today?`
'''
# import Template
from string import Template

# Open external file
file = open('template.txt')

# Create a template from the file
mytemplate = Template(file.read())

# print the parsed template substituting coded vars 
# with corresponding values of a dict
print(mytemplate.substitute({'name': 'Dim'}))