首页 文章

UnicodeDecodeError:'ascii' codec无法解码位置0中的字节0xe7:序数不在范围内(128)

提问于
浏览
15

我在utf-8编码字符时遇到了麻烦 . 我正在使用Django,当我尝试发送带有非纯文本的Android通知时,我收到此错误 . 我试图找到错误来源的位置,我设法弄清楚错误的来源不在我的项目中 .

在python shell中,我键入:

'ç'.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)

我得到同样的错误:

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

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

这是Python,我的操作系统还是其他问题?

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

2 回答

  • 20

    您正在尝试编码/解码字符串,而不是Unicode字符串 . 以下陈述有效:

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

    使用 u'...' ,没有 u 前缀,它是字节串而不是unicode字符串:

    >>> u'ç'.encode('utf8')
    '\xc3\xa7'
    

相关问题