Skip to content

Instantly share code, notes, and snippets.

@romach
Created April 2, 2017 20:06
Show Gist options
  • Save romach/9704fc225a7e040b2c854c4a16f25aca to your computer and use it in GitHub Desktop.
Save romach/9704fc225a7e040b2c854c4a16f25aca to your computer and use it in GitHub Desktop.
Create file by template
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
$title
...
$subtitle
...
$list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment