[Tutor] question !

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Dec 20 22:09:17 CET 2005



On Mon, 19 Dec 2005, Krava Magare wrote:

>  How can I remove and add record ( dictionary type) to a file.

Hi Krava,

Hmmmm... I have to admit that I don't understand the question yet.
*grin*


It looks like you're already pickling lists into your file.  Picking
dictionaries should be similar; are you running into problems with this,
and if so, can you show us?


Some more comments on your program:

>   def  write_file():
>       CIT101 = ["Academic Computer Skills"]
>       CIT111 = ["Database Management"]
>       CIT115 = ["Intro to Computer scince"]
>       CIT127 = ["ACCESS"]
>       CIT211 = ["Systems Analysis and Design"]
>       CIT216 = ["Visual Basic"]
>       CIT218 = ["Intermediate Visual Basic"]
>       CIT234 = ["Decision Support Using Excel"]

This does feel like a place where a dictionary would come in very handy:
The program uses names of the form 'CIT???' which I am assuming are
academic class identifier name, and you're trying to store a relationship
between those names and their descriptsions.  That's a key-value thing.


>       pickle_file.close

Don't forget that Python requires parentheses to make function calls fire
off.  The above call to close doesn't do anything yet: you need parens:

    pickle_file.close()



>   def dele_file():
>       word_dele = raw_input("Which record do u want to delete?: ")
>
>       if word_dele in picles.keys():
                        ^^^^^^

This is misspelled.


Also, the del call on the next statement:

>           del word_dele

is ineffective because it only drops the local name 'word_dele' and does
not affect your pickle.


You probably meant to write:

    del pickles[word_dele]

instead.



It looks like you're getting mixed up when you're using pickles.  Mixing
the 'pickle' and 'shelve' modules together is confusing and something you
may want to avoid.

If you're used to using dictionaries, I'd strongly recommend just sticking
with the 'shelve' module for persistance: it provides a dictionary-like
interface that is fairly straightforward.  You can even say something
like:

    pickles['academic_classes'] = {'CIT101' : 'Academic Computer Skills'}

The values that we store in shelves can be Python data structures: they're
not limited to numbers.



If you have more questions, please feel free to ask.



More information about the Tutor mailing list