[英]Python 3 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
I'm implementing this notebook on Windows with Python 3.5.3 and got the follow error on load_vectors() call. 我正在使用Python 3.5.3在Windows上实现这个笔记本 ,并在load_vectors()调用时得到了关注错误。 I've tried different solutions posted but none worked. 我尝试过不同的解决方案,但都没有效果。
<ipython-input-86-dd4c123b0494> in load_vectors(loc)
1 def load_vectors(loc):
2 return (load_array(loc+'.dat'),
----> 3 pickle.load(open(loc+'_words.pkl','rb')),
4 pickle.load(open(loc+'_idx.pkl','rb')))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
I solved this issue by copying and pasting the entire csv file into text and reading it with: 我通过将整个csv文件复制并粘贴到文本中并使用以下方法读取它来解决此问题:
with open(self.path + "/review_collection.txt", "r", encoding="utf-8") as f:
read = f.read().splitlines()
for row in read:
print(row)
You should probably give encoding for pickle.load(f, encoding='latin1')
, but please make sure all the characters in your file will follow the encoding. 您应该为pickle.load(f, encoding='latin1')
提供编码,但请确保文件中的所有字符都遵循编码。
By default, your pickle code is trying to decode the file with 'ASCII' which fails. 默认情况下,您的pickle代码正在尝试使用'ASCII'解码文件失败。 Instead you can explicitly tell which one to use. 相反,您可以明确告诉使用哪一个。 See this from Documentation . 从文档中查看。
If latin1 doesn't solve, try with encoding='bytes'
and then decode all the keys and values later on. 如果latin1没有解决,请尝试使用encoding='bytes'
然后解码所有键和值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.