[英]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.