UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5

Haris Bašić picture Haris Bašić · Sep 18, 2013 · Viewed 18.8k times · Source
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 537: ordinal not in range(128), referer: ...

I always get this error when I try to output my whole website with characters "č". I am using mako templating. What to do?

Answer

Antti Haapala picture Antti Haapala · Sep 18, 2013

The error occurs because somewhere code coerces your unicode template string into a python 2 str; you need to encode the rendered template into an UTF-8 bytestring yourself:

if isinstance(rendered, unicode):
    rendered = rendered.encode('UTF-8')

# rendered is now guaranteed to be of type str