[英]UnicodeDecodeError: 'ascii' codec can't 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 output my whole website with characters "č". 当我尝试使用字符“č”输出整个网站时,我总是收到此错误。 I am using mako templating.
我正在使用mako模板。 What to do?
该怎么办?
The error occurs because somewhere code coerces your unicode template string into a python 2 str
; 发生错误是因为某处代码将您的unicode模板字符串强制转换为python 2
str
; you need to encode the rendered template into an UTF-8 bytestring yourself: 您需要自己将渲染的模板编码为UTF-8字节串:
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: 问题是您的代码因为超过8位而无法解码某些字符,因此请尝试使用此代码:
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
. 文件:
man locale
, man setlocale
。
For Linux, also install a language pack, eg sudo apt-get install language-pack-en
. 对于Linux,还要安装语言包,例如
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. 如果您正在网站上工作,您可以为所有特殊字符创建一个sanytize功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.