“for line in…” results in UnicodeDecodeError: 'utf-8' codec can't decode byte

問題:

Here is my code,這是我的代碼,

for line in open('u.item'):
# Read each line

Whenever I run this code it gives the following error:每當我運行此代碼時,它都會出現以下錯誤:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 2892: invalid continuation byte UnicodeDecodeError: 'utf-8' 編解碼器無法解碼位置 2892 中的字節 0xe9:繼續字節無效

I tried to solve this and add an extra parameter in open().我試圖解決這個問題並在 open() 中添加一個額外的參數。 The code looks like:代碼如下:

for line in open('u.item', encoding='utf-8'):
# Read each line

But again it gives the same error.但它再次給出了同樣的錯誤。 What should I do then?那我該怎麼辦?


解決方案:

參考一: https://en.stackoom.com/question/1Kehj
參考二: https://stackoom.com/question/1Kehj
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章