Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: a bytes-like object is required, not 'str' when opening Python 2 Pickle file in Python 3

I am trying to open a pickle file in Python 3 with code that worked in Python 2 but is now giving me an error. Here is the code:

with open(file, 'r') as f:
    d = pickle.load(f)

TypeError                                 Traceback (most recent call last)
<ipython-input-25-38f711abef06> in <module>()
      1 with open(file, 'r') as f:
----> 2     d = pickle.load(f)

TypeError: a bytes-like object is required, not 'str'

I saw on other SO answers that people had this problem when using open(file ,'rb') and switching to open(file ,'r') fixed it. If this helps, I tried open(file ,'rb') just to experiment and got the following error:

UnpicklingError                           Traceback (most recent call last)
<ipython-input-26-b77842748a06> in <module>()
      1 with open(file, 'rb') as f:
----> 2     d = pickle.load(f)

UnpicklingError: invalid load key, '\x0a'.

When I open the file with f = open(file, 'r') and the enter f I get:

<_io.TextIOWrapper name='D:/LargeDataSets/Enron/final_project_dataset.pkl' mode='r' encoding='cp1252'>

So I also tried:

with open(file, 'rb') as f:
    d = pickle.load(f, encoding='cp1252')

and got the same error as with using 'rb':

UnpicklingError                           Traceback (most recent call last)
<ipython-input-27-959b1b0496d0> in <module>()
      1 with open(file, 'rb') as f:
----> 2     d = pickle.load(f, encoding='cp1252')

UnpicklingError: invalid load key, '\x0a'.
like image 873
jss367 Avatar asked Sep 01 '17 14:09

jss367


People also ask

How do I fix Typeerror a bytes-like object is required not str in Python?

Binary files are considered a series of bytes data and not as a string. It means that all data read from the file is returned as bytes objects, not str. We can solve this error by opening the file in read-only mode instead of binary mode, as shown below.

How do you solve a bytes-like object is required not str?

Bytes-Like Object Similar Error You may encounter this error if you try to use a string method on a list of bytes. To solve this error, you can use the same approach that we used to solve the last error. Make sure that you open up any text files in text read mode instead of binary read mode.

What is a bytes-like object in Python?

Bytes-like object in python In Python, a string object is a series of characters that make a string. In the same manner, a byte object is a sequence of bits/bytes that represent data. Strings are human-readable while bytes are computer-readable. Data is converted into byte form before it is stored on a computer.

What is Pickle load?

pickle. load is used to load pickled data from a file-like object. This is any object that acts like a file - in this case, meaning it has a read() method that returns bytes . For example: import pickle with open("myobj.pickle", "rb") as f: myobj = pickle.load(f)


Video Answer


1 Answers

Explanation for loading with encoding = bytes.

Assume you have a dictionary to be pickled in Python2

data_dict= {'key1': value1, 'key2': value2}
with open('pickledObj.pkl', 'wb') as outfile:
  pickle.dump(data_dict, outfile)

Unpickling in Python3

with open('pickledObj.pkl', 'rb') as f:
        data_dict = pickle.load(f, encoding='bytes')

Note: The keys of dictionary are not strings anymore. They are bytes.

data_dict['key1'] #result in KeyError

data_dict[b'key1'] #gives value1

or use

data_dict['key1'.encode('utf-8')] #gives value1
like image 192
Sreeragh A R Avatar answered Oct 19 '22 18:10

Sreeragh A R