使用utf8编码/解码错误

2024-06-17 15:45:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我该如何正确编码以下内容:

# # -*- coding: utf-8 -*-

>>> 'What\x80\x99s Up: Balloon to the Rescue!'.encode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 4: ordinal not in range(128)
>>> 'What\x80\x99s Up: Balloon to the Rescue!'.decode('utf-8')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 4: invalid start byte

Tags: thetoinlinebytewhatutffile
2条回答
^{1}$

所以你不能对它进行编码,因为它不是Unicode。在

你的Unicode输入是什么?在

你有两个问题。首先,您的UTF-8字节序列是错误的;它应该是\xe2\x80\x99。您还使用了错误的函数;您需要从UTF-8解码该函数:

^{1}$

相关问题 更多 >