Created
November 6, 2018 15:04
-
-
Save dagg/8f16eab96417a5d2fd4713aeb2abc5ac to your computer and use it in GitHub Desktop.
Python Templates using external file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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