shelve and nested dictionaries

Matthew Schibler m.schibler at gmail.com
Thu Jan 3 03:14:43 EST 2008


I'm a newbie to Python, with some experience using perl (where I used
nested arrays and hashes extensively). I am building a script in
python for a MUD I play, and I want to use the shelve module to store
persistent information between script executions. The following code
does not work for me,

import shelve, sys, os, string
db = shelve.open(os.path.abspath(os.path.dirname(sys.argv[0])) + '/' +
'sandbox.dat', 'c')
db['JustSomeVariable'] = 'apple'
db['subdb'] = {}
db['subdb']['anotherdict'] = {}
db['subdb']['anotherdict']['bleh'] = 'hello world'
db.close()

of course, that's just a working example but it illustrates the
problem i'm having. I think shelve objects act like dictionaries in a
way, at least they seem to have dictionary keys beneath them. And I
don't seem to have this problem when I use a normal dictionary as
opposed to shelve for nesting other dictionaries.

So i'm now confused, i've hit a brick wall and i'm not sure how to
solve this problem.

Can anyone explain what i'm doing wrong?

Thanks



More information about the Python-list mailing list