python裏面字符的編碼方式很多,一不小心就搞錯了
嘗試從txt裏讀取一個爬取的英文小故事,並用nltk庫對詞進行篩選。讀取的方式是readline函數,結果再文章末尾出現了報錯:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x9d in position 33: illegal multibyte sequence
後來想了下,乾脆改成utf-8編碼試一試,結果更慘……直接就在第一句話被暴斃了
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 283: invalid start byte
最後找了很久,嘗試了一個如下的編碼,終於解決了問題:
with open(file, encoding='ISO-8859-1' ) as fin:
希望能夠幫到更多的人吧