pickle.load错误:UnicodeDecodeError:'utf8'编解码器无法解码字节……无效的起始字节

时间:2018-06-26 21:54:45

标签: python pickle

我有一本包含以下内容的numpy数组的pickle文件字典:

{'test': array([[-0.18496349, -0.45449205, -0.48359153, ..., -0.04512954,
    -0.07903713, -0.22708473],
   [ 0.56156779,  1.7704169 ,  1.64386864, ..., -0.04512954,
    -0.07903713, -0.22708473],
   [-1.40342229, -1.13582737, -1.07386949, ..., -0.04512954,
    -0.07903713, -0.22708473],
   ...,
   [ 0.39234589, -0.6842755 , -0.58197119, ..., -0.04512954,
    -0.07903713, -0.22708473],
   [-0.1559453 , -0.3456116 , -0.21304746, ..., -0.04512954,
    -0.07903713, -0.22708473],
   [-0.18435258, -0.68987475, -0.70494576, ..., -0.04512954,
    -0.07903713, -0.74499316]])}

我可以在Python 3.6中成功读取pickle文件,如下所示:

with open('test.pkl', 'rb') as f:
    data = pickle.load(f)

然后我将要在Python 2.7中读取的数据写为:

with open('test_2.pkl', 'wb') as f:
    pickle.dump(data, f, protocol=2)

然后,当我尝试以以下方式在Python 2.7中读取文件时:

with open('test_2.pkl', 'rb') as f:
    data = pickle.load(f)

我收到此错误: UnicodeDecodeError: 'utf8' codec can't decode byte 0x86 in position 2576890457: invalid start byte

在保存泡菜文件之前,我试图将data字典中的键转换为ascii或unicode,但它没有改变错误。

更新: numpy数组的dtype是float64,我注意到,如果将numpy数组强制转换为float32,则可以在Python 2.7中加载文件。 float64在这里有什么问题吗?

0 个答案:

没有答案