Warm tip: This article is reproduced from stackoverflow.com, please click
python-3.x encoding io pickle dill

how to make dill load python2 pickle in python3

发布于 2020-03-27 10:16:54

If pickled using pickle then following snippet works. but if I have an object dumped using dill, dill.load does not work because dill.load does not accept any encoding argument. is there any way to make this work using dill?

with open(‘py2pickle.p’,'rb') as f
    data = pickle.load(f, encoding='latin1')
Questioner
muon
Viewed
100
Mike McKerns 2019-07-19 20:39

You did dill.dump in python 2, and want do do a dill.load in python 3. There are two issues:

  1. there's no guarantee, regardless of what you are using for serialization that a pickle will work with any version of python other than the one you used (i.e. 3.7 vs 3.6 vs 2.7).

  2. as you noted, currently dill doesn't have an encoding argument on load, so you may have to do some conversion before/after you dump/load the object (directly on the object itself).

Note that I will be adding more of the serialization option arguments to dump and load in the very near future (including the encoding argument).

Update: dill now has an encoding argument, as well as other arguments to aid pickle conversion from 2.x to 3.x.