Skip to content

Instantly share code, notes, and snippets.

@dagg
Created November 6, 2018 15:04
Show Gist options
  • Save dagg/8f16eab96417a5d2fd4713aeb2abc5ac to your computer and use it in GitHub Desktop.
Save dagg/8f16eab96417a5d2fd4713aeb2abc5ac to your computer and use it in GitHub Desktop.
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'}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment