Created
April 2, 2017 20:06
-
-
Save romach/9704fc225a7e040b2c854c4a16f25aca to your computer and use it in GitHub Desktop.
Create file by template
This file contains hidden or 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
from string import Template | |
#open the file | |
file = open( 'foo.txt' ) | |
#read it | |
template = Template( file.read() ) | |
#document data | |
title = "This is the title" | |
subtitle = "And this is the subtitle" | |
list = ['first', 'second', 'third'] | |
file_content={ 'title':title, 'subtitle':subtitle, 'list':'\n'.join(list) } | |
#do the substitution | |
result = template.substitute(file_content) | |
print result |
This file contains hidden or 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
$title | |
... | |
$subtitle | |
... | |
$list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment