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
    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
    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,保存就可以了。


UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence_第1张图片

如果你已经这样做,还是有编码错误的话,建议把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,并且可以在文本内容首尾加个标签,保存,再将其放在服务器上,通过urilib.request.urlopen爬取到也可以。

你可能感兴趣的:(UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence)