[Tutor] need advice about a dictionary ({})

Richard D. Moores rdmoores at gmail.com
Sun Sep 11 01:18:18 CEST 2011

On Sat, Sep 10, 2011 at 15:32, Alan Gauld <alan.gauld at btinternet.com> wrote:
> On 10/09/11 19:08, Richard D. Moores wrote:
>> Some have suggested using the shelve module. I looked at it but
>> couldn't see in detail how to use it.
> Did you read the help page?

I did. I can see it would be a useful reference once I learned from
elsewhere how to use shelve.

> It says:
> import shelve
> d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
> d[key] = data   # store data at key
> data = d[key]   # retrieve a COPY of the data at key
> del d[key]      # delete data stored at key
> flag = d.has_key(key)   # true if the key exists
> list = d.keys() # a list of all existing keys (slow!)
> d.close()       # close it
> So you open the file and from that point on treat it exactly like a
> dictionary.

I'm still a bit shaky about dictionaries.

> Then close the file at the end.
> Now which part don't you understand?

Much of what comes after that is beyond me.

> The ony bit that migt confuse is the mention of gdbm filename which just
> means give it a filename without any suffix...just ignore the gdbm
> reference.

Thanks for your encouragement Alan, but I'm still looking among my
Python books for a good exposition of shelve.


More information about the Tutor mailing list