On Wed, 8 Aug 2007, TheSarge wrote:

> I have five data files, that are used to build a database.
> 
> 1.txt
> 2.txt
> 3.txt
> 4.text
> 5.txt
> 
> I want to build a database using a persistent dictionary (shelve).
> 
> The specifications are that the key for each dictionary keyword pair, is the
> lefthand side 
> value of the # sign, and the corresponding value for the data is the phrase
> on the 
> righthand side.
> 
> Can someone help me manipulate this please? I can't not get a grasp on
> shelves and on what I need to do.

Basically, once you create a shelf object (which you do by calling 
shelve.open on a new or existing filename), you treat it like a 
dictionary.  When you close it, the values in the dictionary are saved.

For example, in one program, you would have:


shelf = shelve.open("testfile.shelf")

And later, lines like:

shelf["a"] = "Alligators all around"     # just like a dictionary.
shelf["b"] = "Bursting balloons"
 . . .
shelf["z"] = "Zippety Zound"

and eventually:

shelf.close()


Then, in another program (or later in the same program) you could re-open 
the shelf-file and use it as a dictionary

shlf = shelv.open("testfile.shelf")

for key in shlf:
   print key, shlf[key]

and you should see, in arbitrary order, things like:

b Bursting balloons
r Riding reindeer
e Entertaining Elephants
a Aligators all around


Is this a homework problem, or a real-life application?

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to