UnicodeDecodeError:'ascii'编解码器无法解码位置6的字节0xf2:序数不在范围内(128)

时间:2019-03-14 14:21:17

标签: python python-requests pickle spyder python-3.7

我正在尝试使用.pkl文件。

这是我的代码

import pickle
with open('C:\\Users\\Utilizador\\Desktop\\teste\\teste.pkl', 'rb') as f:
    data = pickle.load(f)

它给了我以下错误:

  File "C:/Users/Utilizador/Desktop/teste/untitled0.py", line 4, in <module>
    data = pickle.load(f)    
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf2 in position 6: ordinal not in range(128)

预先感谢

2 个答案:

答案 0 :(得分:0)

对我来说,使用python2代替python3解决了这个问题。我试图从public research dataset加载pkl文件。

Python 3错误:

fbobee@server:~/WESAD/S10$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> with open('S10.pkl', 'rb') as f:
...   data = pickle.load(f)
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf2 in position 6: ordinal not in range(128)

Python 2成功:

fbobee@server:~/WESAD/S10$ python
Python 2.7.12 (default, Nov 12 2018, 14:36:49) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> with open('S10.pkl', 'rb') as f:
...   data = pickle.load(f)
... 
>>> data.keys()
['signal', 'subject', 'label']

我没有在数据中发现任何异物,它包含一些字符串(仅英文字母)和数字。 Documentation says泡菜向后兼容。也许它与new text model of python 3有联系。

答案 1 :(得分:0)

在python 3中,您可以基于 https://rebeccabilbro.github.io/convert-py2-pickles-to-py3/

with open("old_pickle.pkl", 'rb') as f:
    loaded = pickle.load(f, encoding="latin1") 

with open("old_pickle.pkl", 'rb') as f:
    loaded = pickle.load(f, encoding="bytes")