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

泄露秘密 提交于 2019-12-22 04:47:15

问题


I'm implementing this notebook on Windows with Python 3.5.3 and got the follow error on load_vectors() call. 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)

回答1:


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.

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.

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




回答2:


I solved this issue by copying and pasting the entire csv file into text and reading it with:

with open(self.path + "/review_collection.txt", "r", encoding="utf-8") as f:
    read = f.read().splitlines()
    for row in read:
        print(row)



回答3:


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://stackoverflow.com/questions/46046752/python-3-unicodedecodeerror-ascii-codec-cant-decode-byte-0xe2-in-position-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!