UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence

針對我上一篇《簡易版計算文本相似度》出現的問題:

Traceback (most recent call last):
  File "D:/pythonlianxi/wenbensimi1.py", line 52, in <module>
    d3 = open(doc3).read()

UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence

編碼錯誤,好吧,在後面加上encoding='utf-8',問題依然存在。

Traceback (most recent call last):
  File "D:/pythonlianxi/wenbensimi1.py", line 9, in <module>
    d1 = open(doc1,'r',encoding='utf-8').read()
  File "C:\Users\asus\AppData\Local\Programs\Python\Python35\lib\codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 0: invalid start byte

解決方法:因爲打開的都是txt,所以就將文本另存爲,編碼從ANSI變成UTF-8,保存就可以了。



如果你已經這樣做,還是有編碼錯誤的話,建議把d3 = open(doc3,'r',encoding='utf-8').read(),改成d3=urilib.request.urlopen("http://127.0.0.1/zhenhuan.html").read().decode("utf-8"),把文本從txt改成html,並且可以在文本內容首尾加個標籤<html></html>,保存,再將其放在服務器上,通過urilib.request.urlopen爬取到也可以。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章