Question

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?

Was it helpful?

Solution

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

OTHER TIPS

The problem is that your code cannot decode some of characters because of being more than 8 bits so try to use this:

converted = unicode("your_string", encoding="utf-8", errors="ignore")

Good luck

Make sure you're running your script with the right locale settings, e.g.

$ locale -a | grep "^en_.\+UTF-8"
en_GB.UTF-8
en_US.UTF-8
$ export LC_ALL=en_GB.UTF-8
$ export LANG=en_GB.UTF-8

Docs: man locale, man setlocale.

For Linux, also install a language pack, e.g. sudo apt-get install language-pack-en.

You can replace your special characters č with this code: č

"your string".replace('č','č')

if you are working on a web site u can create a sanytize function for all the special characters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top