简体   繁体   English

TypeError:在Python 3中打开Python 2 Pickle文件时,需要一个类似字节的对象,而不是'str'

[英]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. 我试图在Python 3中打开一个pickle文件,其代码在Python 2中有效,但现在给我一个错误。 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. 我在其他SO回答中看到,当使用open(file ,'rb')并切换到open(file ,'r')修复它时,人们遇到了这个问题。 If this helps, I tried open(file ,'rb') just to experiment and got the following error: 如果这有帮助,我尝试open(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(file, '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'.

Explanation for loading with encoding = bytes. 使用encoding = bytes加载的说明。

Assume you have a dictionary to be pickled in Python2 假设您有一个字典可以在Python2中进行腌制

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

Unpickling in Python3 在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

Yeah, there are some changes between the Python 2 and 3 pickle formats. 是的,Python 2和3 pickle格式之间有一些变化。 If possible, I'd recommend creating the pickled data again using Python 3. 如果可能的话,我建议使用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' ?)或者使用这里提到的encoding='bytes'读取数据,然后解码你可以检查对象的代码中的字符串进一步。

After digging through the raw file in Sublime, it looks like the file was not correctly pickled. 在Sublime中挖掘原始文件之后,看起来文件没有被正确地腌制。 The above code works perfectly on a different version of that file. 上面的代码完全适用于该文件的不同版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要一个类似字节的对象,在pickle python 3.6中不需要&#39;str&#39; - a bytes-like object is required, no 'str' with pickle python 3.6 错误消息:TypeError:需要一个类似字节的对象,而不是在Python中使用Pickle接收到的“ str” - Error message: TypeError: a bytes-like object is required, not 'str' received using Pickle in Python “TypeError:在 Python 3 中处理文件内容时,需要类似字节的 object,而不是‘str’” - "TypeError: a bytes-like object is required, not 'str'" when handling file content in Python 3 类型错误:在 Python3.x 中读取和迭代文件时需要一个类似字节的对象,而不是“str” - TypeError: a bytes-like object is required, not 'str' when reading and iterating over a file in Python3.x Python TypeError: a bytes-like object is required, not 'str' 在尝试下载文件时 - Python TypeError: a bytes-like object is required, not 'str' when trying to download a file TypeError:需要一个类似字节的对象,而在用pickle加载时不是&#39;str&#39; - TypeError: a bytes-like object is required, not 'str' while loading with pickle TypeError:需要一个类似字节的对象,而不是&#39;str&#39;python - TypeError: a bytes-like object is required, not 'str' python TypeError:需要一个类似字节的对象,而不是 Python 中 Image 命令的“str” - TypeError: a bytes-like object is required, not 'str' for Image command in Python Python TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - Python TypeError: a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是&#39;str&#39;python3 - TypeError: a bytes-like object is required, not 'str' python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM