问题
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?
回答1:
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
回答2:
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
回答3:
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
.
回答4:
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.
来源:https://stackoverflow.com/questions/18877589/unicodedecodeerror-ascii-codec-cant-decode-byte-0xc5