在 python2 中加载 pickle 文件(来自 python3)

Load pickle file(comes from python3) in python2

我有一个 pickle 文件,

>>> with open("wikilinks.pickle", "rb") as f:
...     titles, links = pickle.load(f)
... 
>>> len(titles)
13421

我可以在 python3 中加载它。 但是,当我尝试在 python2 中加载它时,我收到以下消息: 回溯(最近调用最后):

  File "<stdin>", line 2, in <module>
  File "/usr/lib/python2.7/pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 886, in load_proto
    raise ValueError, "unsupported pickle protocol: %d" % proto
ValueError: unsupported pickle protocol: 3

那么如何在python2中加载呢?

Python3中默认使用的default pickling protocol与Python2使用的协议不兼容,但是如果可以修改产生wikilink.pickle的代码,您可以告诉它使用较早的协议之一(0、1 或 2),Python 2 会很好地理解这些协议。

不过请注意,Python 3 个字符串将作为 Unicode 字符串出现在 Python 2 中,这在某些用例中可能会令人惊讶。

$ python3
Python 3.4.1 (default, May 19 2014, 17:23:49) 
[GCC 4.9.0 20140507 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.dumps([1, 2, 'abc'], 2)
b'\x80\x02]q\x00(K\x01K\x02X\x03\x00\x00\x00abcq\x01e.'
>>> 
$ python 
Python 2.7.8 (default, Jul  1 2014, 17:30:21) 
[GCC 4.9.0 20140604 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cPickle
>>> cPickle.loads('\x80\x02]q\x00(K\x01K\x02X\x03\x00\x00\x00abcq\x01e.')
[1, 2, u'abc']

读入python3并保存为python2格式

#!/usr/bin/env python3

import pickle

with open("a.pkl", "rb") as f:
    w = pickle.load(f)

pickle.dump(w, open("a_py2.pkl","wb"), protocol=2)

然后你可以在python2中加载a_py2.pkl