[谁能建议我如何解决此错误。我只是在加载手套矢量,并且尝试进行迭代时,它显示此错误
embeddings_index = dict()
f = open('/content/drive/My Drive/lstm donor/lstm_glove_vectors')
for line in f:
values = line.split()
word = values[0]
coefs = asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
f.close()
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-79-3373015fdc0b> in <module>()
1 embeddings_index = dict()
2 with open('/content/drive/My Drive/lstm donor/lstm_glove_vectors','r',encoding='utf-8') as f:
----> 3 for line in f:
4 values = line.split()
5 word = values[0]
/usr/lib/python3.6/codecs.py in decode(self, input, final)
319 # decode input (taking the buffer into account)
320 data = self.buffer + input
--> 321 (result, consumed) = self._buffer_decode(data, self.errors, final)
322 # keep undecoded input until the next call
323 self.buffer = data[consumed:]
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
这是一个解码问题,即它无法解码要读取的数据。如果尝试读取csv,请在文件名中添加csv
。
好像是在说文件的第一个字节(位置0)是0x80,除非这意味着在解码单个字符的某个时候位置0。无论如何,这意味着它不是有效的utf-8文件。我不认识名字'lstm_glove_vectors',所以有人训练了他们自己的向量,或者对原始的分布式向量做了一些事情(至少是重命名,也许还有更多处理)。该文件很可能不是纯文本文件。可能是gzip压缩文件或zip文件?还是二进制编码的矢量作为数字?
[我只是尝试用more
或less
命令之类的内容查看内容,然后查看其中似乎有什么。
最终可能性:Common Crawl衍生的GloVe向量的第一个发行版确实确实存在一些Unicode错误,因此如果您使用的是非常旧的数据文件,则可能会发生这种情况。但该问题已在2015年解决。