[Tutor] Address book sort of
Rick Muller
rick_muller at yahoo.com
Sat Dec 4 14:28:11 CET 2004
Couldn't tell if this got sent, so I'm re-sending.
Apologies for duplicates:
Well, one option is to use pickle (or cPickle,
preferrably) to dump the python objects to a file:
from cPickle import load, dump
def save(fname,addressbook):
file = open(filename,'w')
dump(addressbook,file)
file.close()
return
def read(fname):
file = open(filename)
addressbook = load(file)
file.close()
return addressbook
The advantage of pickle is that you don't have to
decide on a text format for your data -- it just dumps
and then reloads the python code. You can waste a lot
of time deciding on a text format, implementing the
readers/writers, etc.
Rick
--- Eri Mendz
<"erimendz at bluebottle.com"@bluebottle.com> wrote:
> Dear Tutor,
>
> I like to know what is the proper procedure (is
> algorithmn the right
> term?) in creating data in a program, write it to
> file, close the app
> then retrieve the data when run again. Basically,
> I'm trying to simulate
> a simple address book (well not really for the datas
> are just names for
> now) and so far have created the basic menu
> interface. It is console
> base so forget gui. I ask user input and store it in
> a list. There are
> menus to change, delete the data, and to save the
> data list in file. I
> use cPickle for this and have verified the file is
> created by checking
> in my $PWD. I want to retrieve that data when
> program is run again. What
> to add in my code? I thought not to post the code
> but explain it as
> above.
>
> What i want: when program is run again, the saved
> data is loaded when user
> selects option 1 below. Of course the first time it
> is run, the list is
> empty.
>
> def print_options():
> print '''
> Options:
> [1] - Print content of list
> [2] - Add name to list
> [3] - Delete name from list
> [4] - Change name in list
> [5] - Save list to file
> [P] - Print this menu
> [Q] - Quit
> '''
>
>
>
> --
> Regards,
> Eri Mendz
> Using PC-Pine 4.61
>
>
> --
> Using PC-Pine 4.61
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
=====
Rick Muller
rick_muller at yahoo.com
__________________________________
Do you Yahoo!?
Yahoo! Mail - Find what you need with new enhanced search.
http://info.mail.yahoo.com/mail_250
More information about the Tutor
mailing list