shelve / pickle error


V

Vineet

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.
 
Ad

Advertisements

P

Peter Otten

Vineet said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top