1

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?

Nijin22
  • 850
  • 13
  • 20

1 Answers1

1

Make all strings unicode, including strings from the file:

data = [{"variable" : u"äöü"}]

templateFile = codecs.open('template.txt', 'r', encoding='utf-8')

filledFile = codecs.open('rendered.txt', 'w', encoding='utf-8')

Get the result using unicode(), not str().

This is not required but recommended — add #encoding utf-8 to the template:

#encoding utf-8
This is static text in the template: äöü
This is filled by Cheetah: $variable

See examples in Cheetah tests: https://github.com/CheetahTemplate3/cheetah3/blob/master/Cheetah/Tests/Unicode.py.

phd
  • 82,685
  • 13
  • 120
  • 165
  • 1
    Thank you! To add to this answer: When using python3, You don't need to use `codecs` (as the encoding parameter is provided in `open` as well. You also don't need to mark the string with a `u`. Therefore you only need to provide `encoding` on every file-opening operation. – Nijin22 Mar 10 '18 at 21:05
  • I'm still using Python 2.7 and write code in a portable way. :-) – phd Mar 10 '18 at 21:28