UnicodeDecodeError: codec 'ascii' cannot decode byte 0xc5

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 display my entire site with the characters "č". I am using mako templates. What to do?

+7
python mako mod-wsgi
source share
4 answers

The error occurs because somewhere the code forces your unicode pattern string to python 2 str ; you need to encode the rendered template in UTF-8 yourself:

 if isinstance(rendered, unicode): rendered = rendered.encode('UTF-8') # rendered is now guaranteed to be of type str 
+9
source share

The problem is that your code cannot decode some characters due to more than 8 bits, so try using this:

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

Good luck.

0
source share

Make sure you are using a script with the correct 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 

Documents: man locale , man setlocale .

For Linux, also install a language pack, for example. sudo apt-get install language-pack-en .

0
source share

You can replace the special characters č with the following code: č

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

If you work on a website, you can create a sanytize function for all special characters.

-one
source share

All Articles