简体   繁体   English

UnicodeDecodeError:'ascii'编解码器无法解码字节0xc5

[英]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 localeman 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.

相关问题 Python 3 UnicodeDecodeError:“ascii”编解码器无法解码字节 0xc2 - Python 3 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 Django / Python编码-'ascii'编解码器无法解码位置52'的字节0xc5 - Django/Python encoding - ''ascii' codec can't decode byte 0xc5 in position 52' UnicodeDecodeError: 'ascii' 编解码器无法解码字节 0xc2 - UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 UnicodeDecodeError:“ ascii”编解码器无法解码字节 - UnicodeDecodeError: 'ascii' codec can't decode byte UnicodeDecodeError:'ascii'编解码器无法解码位置23的字节0xc3:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 23: ordinal not in range(128) UnicodeDecodeError:'ascii'编解码器无法解码位置40中的字节0xc3:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 40: ordinal not in range(128) UnicodeDecodeError:'ascii'编解码器无法解码位置5的字节0xc3:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128) UnicodeDecodeError:“ascii”编解码器无法解码位置 1 的字节 0xc4:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 1: ordinal not in range(128) UnicodeDecodeError:'ascii'编解码器无法解码位置7的字节0xc7:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xc7 in position 7: ordinal not in range(128) UnicodeDecodeError:'ascii'编解码器无法解码位置19的字节0xc3:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 19: ordinal not in range(128)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM