Python v3 question related to pickle/marshal

Peter Otten __peter__ at web.de
Thu Mar 26 18:53:17 CET 2009


Detlev Offenbach wrote:

> is it possible to marshal or pickle a data structure using Python v3 and
> unmarshal or unpickle it using Python v2. If it is possible, how do I
> have to do it. Everything I tried resulted in an EOFError exception upon
> unmarshalling/unpickling.

It seems to work here, at least for simple data:

$ python3.0
Python 3.0 (r30:67503, Dec  4 2008, 11:26:28)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.dump(dict(a=1, b=[42], c="yadda"), open("tmp.p", "wb"),
protocol=2)
>>>
$ python
Python 2.5.1 (r251:54863, Jul 31 2008, 23:17:43)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.load(open("tmp.p"))
{u'a': 1, u'c': u'yadda', u'b': [42]}

Maybe you have incompatible data structures?

Peter



More information about the Python-list mailing list