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

Alan Gauld alan.gauld at btinternet.com
Sun Sep 11 00:32:11 CEST 2011


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

Then close the file at the end.

Now which part don't you understand?
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.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list