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?
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
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, eg
$ 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, eg 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.