UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 537: ordinal not in range(128), referer: ...
当我尝试输出包含 "č" 字符的整个网站时,经常会遇到这个错误。我正在使用 mako 模板。该怎么办?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 537: ordinal not in range(128), referer: ...
当我尝试输出包含 "č" 字符的整个网站时,经常会遇到这个错误。我正在使用 mako 模板。该怎么办?
str
类型导致的;您需要自己将渲染后的模板编码为UTF-8字节数组:if isinstance(rendered, unicode):
rendered = rendered.encode('UTF-8')
# rendered is now guaranteed to be of type str
converted = unicode("your_string", encoding="utf-8", errors="ignore")
祝好运
$ 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
文档: man locale
,man setlocale
。
对于Linux系统,还需安装语言包,例如:sudo apt-get install language-pack-en
。
您可以使用此代码替换特殊字符č:č
"your string".replace('č','č')