今天就跟大家聊聊有關UnicodeDecodeError: 'utf8'的解決方法是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd4 in position x: invalid continuation byte
更多0
pythonencode字符串
在執行下面語句時會報錯
o = "a test of \xe9 char" #I want this to remain a string as this is what I am receiving
v = o.decode("utf-8")
錯誤堆棧信息如下:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\encodings\utf_8.py",
line 16, in decode
return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError:
'utf8' codec can't decode byte 0xe9 in position 10: invalid continuation byte
這是因為\xe9不是合法的utf-8字符,不符合utf-8的定義,對于這種情況可以這么處理:
o = "a test of \xe9 char" #I want this to remain a string as this is what I am receiving
v = o.decode('latin-1').encode("utf-8")
print v
問題
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte解決方法
解決:
這是因為\xe9不是合法的utf-8字符,不符合utf-8的定義,對于這種情況可以這么處理:
li.append(line.decode('utf-8','ignore
看完上述內容,你們對UnicodeDecodeError: 'utf8'的解決方法是什么有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。