UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5

╄→гoц情女王★ 提交于 2019-12-23 09:26:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!