UnicodeDecodeError:'ascii'编解码器无法解码位置0的字节0xe7:序数不在范围内(128) [英] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)

查看:84
本文介绍了UnicodeDecodeError:'ascii'编解码器无法解码位置0的字节0xe7:序数不在范围内(128)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在utf-8中编码字符时遇到麻烦.我使用的是Django,尝试发送非纯文本的Android通知时收到此错误.我试图找到错误的来源,然后设法找出错误的来源不在我的项目中.

I'm having troubles in encoding characters in utf-8. I'm using Django, and I get this error when I tried to send an Android notification with non-plain text. I tried to find where the source of the error and I managed to figure out that the source of the error is not in my project.

在python shell中,我输入:

In python shell, I type:

'ç'.encode('utf8')

我得到这个错误:

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)

我也遇到了相同的错误:

I get the same errors with:

'á'.encode('utf-8')
unicode('ç')
'ç'.encode('utf-8','ignore')

我也遇到了smart_text,force_text和smart_bytes错误.

I get errors with smart_text, force_text and smart_bytes too.

是Python,我的操作系统或其他问题吗?

Is that a problem with Python, my OS, or another thing?

我正在Red Hat版本4.4.7-3上运行Python 2.6.6

I'm running Python 2.6.6 on a Red Hat version 4.4.7-3

推荐答案

您正在尝试对字符串(而非Unicode字符串)进行编码/解码.以下语句可以起作用:

You're trying to encode / decode strings, not Unicode strings. The following statements do work:

u'ç'.encode('utf8')
u'á'.encode('utf-8')
unicode(u'ç')
u'ç'.encode('utf-8','ignore')

这篇关于UnicodeDecodeError:'ascii'编解码器无法解码位置0的字节0xe7:序数不在范围内(128)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆