Use python 2 shelf in python 3

后端 未结 4 2125
醉酒成梦
醉酒成梦 2021-02-06 10:00

I have data stored in a shelf file created with python 2.7

When I try to access the file from python 3.4, I get an error:

>>> import shelve
>         


        
4条回答
  •  春和景丽
    2021-02-06 10:54

    The shelve module uses Python's pickle, which may require a protocol version when being accessed between different versions of Python.

    Try supplying protocol version 2:

    population = shelve.open('shelved.shelf', protocol=2)
    

    According to the documentation:

    Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of new-style classes. Refer to PEP 307 for information about improvements brought by protocol 2.

    This is most likely the protocol used in the original serialization (or pickling).

提交回复
热议问题