shelve / pickle error
Peter Otten
__peter__ at web.de
Fri Jun 18 05:15:46 EDT 2010
Vineet wrote:
> Hi !
> I am using python ver 2.6.5
> Trying to use shelve to save an object on the disc.
> =================================
> In the code----
> # There are 'Person' & 'Manager' classes.
> # I created instance objects a,b,c from these classes.
>
> from person import Person, Manager
> a = Person('A A')
> b = Person('B B', job = 'clerk', pay=5000)
> c = Manager('C C', 15000)
>
> # instance objects a,b,c were created correctly.
> # then I store them on a shelve
>
> import shelve
> dbs = shelve.open('mydb')
> for x in (a, b, c):
> dbs[x.name] = x
> dbs.close()
>
> # On the disc, I can see the 3 files viz. mydb.dat, mydb.bak, mydb.dir
> # dbs.keys() is also giving me the correct list of keys.
> ==================================
> # NOW THE PROBLEM AREA ---
> # when I try to fetch an object by key---
>
> a = dbs['A A']
>
> # I get an error as--
> # File "F:\Py26\lib\shelve.py", line 122, in __getitem__
> # value = Unpickler(f).load()
> # ValueError: unsupported pickle protocol: 3
> =============================
> I couldn't locate the bug in this.
> Can anybody pl. point out?
>
> ---Thanks,
> Vineet.
Have you saved the data using Python 3? Try loading it using Python 3, too.
You can also try explicitly specifying a protocol understood by both Python
2 and 3, but I expect that you'll run into other incompatibilities.
Peter
More information about the Python-list
mailing list