类型错误:在Python 3中打开Pickle时,需要一个类似bytes的对象,而不是“str”。

时间:2022-03-03 10:16:31

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:

我正在尝试在python3中打开一个pickle文件,代码在python2中工作,但是现在给我一个错误。这是代码:

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:

我还看到了其他的答案,人们在使用open(file,'rb')并切换到open(file,'r')时遇到了这个问题。如果这有帮助,我尝试打开(file,'rb')来进行实验,得到以下错误:

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:

当我打开文件f = open(文件,'r')和输入f我得到:

<_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':

和使用“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'.

3 个解决方案

#1


3  

Explanation for loading with encoding = bytes.

用编码方式加载的解释=字节。

Assume you have a dictionary to be pickled in Python2

假设您有一个在Python2中被pickle的字典。

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

Unpickling in Python3

在unpickle 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

#2


1  

Yeah, there are some changes between the Python 2 and 3 pickle formats. If possible, I'd recommend creating the pickled data again using Python 3.

是的,在Python 2和3 pickle格式之间有一些变化。如果可能的话,我建议使用Python 3再次创建pickle数据。

If that's not possible/easy, try playing with different encoding settings (did you try 'utf8'?) or reading the data in with encoding='bytes' as mentioned here and then decoding the strings in your code where you can inspect the object further.

如果这是不可能的,试着使用不同的编码设置(你试过utf8吗?)或者用编码='bytes'来读取数据,然后在代码中解码字符串,这样你就可以进一步检查对象了。

#3


0  

After digging through the raw file in Sublime, it looks like the file was not correctly pickled. The above code works perfectly on a different version of that file.

在对原始文件进行了深入挖掘之后,看起来文件没有被正确地pickle。上面的代码在该文件的不同版本上运行得很好。

#1


3  

Explanation for loading with encoding = bytes.

用编码方式加载的解释=字节。

Assume you have a dictionary to be pickled in Python2

假设您有一个在Python2中被pickle的字典。

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

Unpickling in Python3

在unpickle 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

#2


1  

Yeah, there are some changes between the Python 2 and 3 pickle formats. If possible, I'd recommend creating the pickled data again using Python 3.

是的,在Python 2和3 pickle格式之间有一些变化。如果可能的话,我建议使用Python 3再次创建pickle数据。

If that's not possible/easy, try playing with different encoding settings (did you try 'utf8'?) or reading the data in with encoding='bytes' as mentioned here and then decoding the strings in your code where you can inspect the object further.

如果这是不可能的,试着使用不同的编码设置(你试过utf8吗?)或者用编码='bytes'来读取数据,然后在代码中解码字符串,这样你就可以进一步检查对象了。

#3


0  

After digging through the raw file in Sublime, it looks like the file was not correctly pickled. The above code works perfectly on a different version of that file.

在对原始文件进行了深入挖掘之后,看起来文件没有被正确地pickle。上面的代码在该文件的不同版本上运行得很好。