7
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?

kenorb
169k96 gold badges712 silver badges796 bronze badges
asked Sep 18, 2013 at 16:31
1

4 Answers 4

9

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
answered Sep 18, 2013 at 16:36
Sign up to request clarification or add additional context in comments.

Comments

0

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

answered Jan 25, 2015 at 8:16

2 Comments

Unfortunately this is the exact opposite of what OP needs to do.
I'm afraid dear Antti. I think I wrote the solution belonged to the answer exactly. He wants his code to skip the Unicode which is more than one byte and this is the exact solution :)
0

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.

answered Sep 26, 2017 at 12:09

Comments

-1

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.

answered Sep 18, 2013 at 17:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.