Skip to content Skip to sidebar Skip to footer

How Do I Take Integer Keys In Shelve?

I want to store an integer key in shelve. But when I try to store integer key in shelve it give me an error Traceback (most recent call last): File './write.py', line 12, in

Solution 1:

In your example the keys in your database will always be integers, so it should work fine to convert them to strings,

data[str(id)] = {"Id": id, "Name": name}

My test code

defshelve_some_data(filename):
    db = shelve.open(filename, flag="c")
    try:
        # note key has to be a string
        db[str(1)]    = "1 integer key that's been stringified" 
        db[str(2)]    = "2 integer key that's been stringified" 
        db[str(3)]    = "3 integer key that's been stringified" 
        db[str(10)]   = "10 integer key that's been stringified"finally:
        db.close()

defwhats_in(filename):
    db = shelve.open(filename, flag="r")
    for k in db:
        print("%s : %s" % (k, db[k]))
    return

filename = "spam.db"
shelve_some_data(filename)
whats_in(filename)

And the output; it works like a dict so it's not sorted.

2 : 2integerkey that's been stringified10 : 10integerkey that's been stringified1 : 1integerkey that's been stringified3 : 3integerkey that's been stringified

Solution 2:

The shelve module uses an underlying database package (such as dbm, gdbm or bsddb) .

A "shelf" is a persistent, dictionary-like object. The difference with "dbm" databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects -- anything that the pickle module can handle. This includes most class instances, recursive data types, and objects containing lots of shared sub-objects. The keys are ordinary strings. The examples section gives you the proof.

This should work. Here's what I do in my code -

import shelve

#Create shelve
s = shelve.open('test_shelf.db')
try:
    s['key1'] = { 'int': 10, 'float':9.5, 'string':'Sample data' }
finally:
    s.close()

#Access shelve
s = shelve.open('test_shelf.db')
try:
    existing = s['key1']
finally:
    s.close()
print existing

UPDATE: You could try pickle module. It is not a key-value database but you can always build your data structure as a key-value pairs and then send it to pickle -

If you have an object x, and a file object f that's been opened for writing, the simplest way to pickle the object takes only one line of code

pickle.dump(x, f)

To unpickle the object again, if f is a file object which has been opened for reading:

x = pickle.load(f)

I hear cPickle is a lot faster than pickle. You can try this if you have lot of data to store.

Post a Comment for "How Do I Take Integer Keys In Shelve?"