0

i'm trying to work with a file .pkl.

Here is my code

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

It gives me this following error:

  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)

Thanks in advance

dlmartins
  • 51
  • 9
  • It is not unicode. Try decoding it with UTF-8. https://stackoverflow.com/questions/6289474/working-with-utf-8-encoding-in-python-source – ech0 Mar 14 '19 at 14:23
  • Possible duplicate of [UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)](https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20) – Sagar P. Ghagare Mar 14 '19 at 14:26
  • It is weird to get this error when reading the file as binary. How was the file produced? – Serge Ballesta Mar 14 '19 at 14:41
  • It was from a dataset, the WESAD. And i need to open that .pkl file. But i don't know how – dlmartins Mar 14 '19 at 14:48
  • Does this answer your question? [UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1](https://stackoverflow.com/questions/10561923/unicodedecodeerror-ascii-codec-cant-decode-byte-0xef-in-position-1) – Gugu72 Mar 27 '20 at 08:23

2 Answers2

0

For me, using python2 instead of python3 solved the problem. I tried to load a pkl file from a public research dataset.

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

Success in 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']

I didn't find anything exotic in the data, it contains a few strings (english letters only) and numbers. Documentation says that pickle is backward compatible. Maybe it has connection to the new text model of python 3.

0

In python 3, you can use the following based on https://rebeccabilbro.github.io/convert-py2-pickles-to-py3/ :

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

or

with open("old_pickle.pkl", 'rb') as f:
    loaded = pickle.load(f, encoding="bytes")
nurandi
  • 1,588
  • 1
  • 11
  • 20