Python 3 UnicodeDecodeError:“ascii”编解码器无法解码位置 0 中的字节 0xe2:序号不在范围内(128)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46046752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:26:45  来源:igfitidea点击:

Python 3 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

pythonpython-3.xpickle

提问by user1569341

I'm implementing this notebookon Windows with Python 3.5.3 and got the follow error on load_vectors() call. I've tried different solutions posted but none worked.

我正在使用 Python 3.5.3 在 Windows 上实现此笔记本,并在 load_vectors() 调用中遇到以下错误。我尝试了不同的解决方案,但都没有奏效。

<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)

采纳答案by Sreenadh T C

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. Instead you can explicitly tell which one to use. See this from Documentation.

默认情况下,您的pickle 代码正在尝试使用“ASCII”对文件进行解码,但失败了。相反,您可以明确告诉使用哪个。从文档中看到这一点。

If latin1 doesn't solve, try with encoding='bytes'and then decode all the keys and values later on.

如果 latin1 没有解决,请尝试使用encoding='bytes',然后稍后解码所有键和值。

回答by raditya gumay

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)

回答by mwilmes

I got the same error as well. I realized that I copy and pasted text from a file that had left and right double-quotes (curly quotes). Once I changed it to the standard straight double-quotes (") the issue was fixed!

我也遇到了同样的错误。我意识到我从一个带有左右双引号(大引号)的文件中复制并粘贴了文本。一旦我将其更改为标准的直双引号 ("),问题就解决了!

See this link for the difference between the quotes: https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html

请参阅此链接以了解引号之间的区别:https: //www.cl.cam.ac.uk/~mgk25/ucs/quotes.html