romach
4/2/2017 - 8:06 PM

Create file by template

Create file by template

$title
...
$subtitle
...
$list
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