I would like to render a variable with Unicode characters, using the Cheetah template engine.
My template file template.txt looks like this:
This is static text in the template: äöü
This is filled by Cheetah: $variable
My program loads that file, and inserts a variable variable:
from Cheetah.Template import Template
data = [{"variable" : "äöü"}]
# open template
templateFile = open('template.txt', 'r')
templateString = templateFile.read()
templateFile.close()
template = Template(templateString, data)
filledText = str(template)
# Write filled template
filledFile = open('rendered.txt', 'w')
filledFile.write(filledText)
filledFile.close()
This creates a file in which the static Unicode characters are fine, but the dynamic ones are replaced by replacement characters.
This is static text in the template: äöü
This is filled by Cheetah: ���
All files are UTF-8, in case that matters.
How can I ensure that the characters are generated correctly?
Make all strings unicode, including strings from the file:
Get the result using
unicode(), notstr().This is not required but recommended — add
#encoding utf-8to the template:See examples in Cheetah tests: https://github.com/CheetahTemplate3/cheetah3/blob/master/Cheetah/Tests/Unicode.py.