[Tutor] How do I save a dictionary to disk? (another newbie question)

Karthik Gurumurthy karthikg@aztec.soft.net
Fri, 11 Jan 2002 11:09:44 +0530


hi!,

i realize that there are atleast 4 ways for achieving persistence
after having a look at the reference.

1.marshal
2.shelve
3.pickle
4.DBM

what are the differences between these modules.
it says pickle is probably more powerful than marshal.

I took a look at the anydbm module
and played around with it.
It also creates a file and allows us to handle 
stuff as if we are using dictionaries.
But then i could'nt set an integer against a key.
it took only strings.

I have used ConfigParser before even there you add a section.
then set values against a name. Then we can read it back from the section.
Yeah it's meant to be used as a properties file.
But how different it is from using DBM files. I know am missing something
atleast wrt DBM. Probably it is capable of more?

thanks,
karthik	



-----Original Message-----
From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
Andy W
Sent: Friday, January 11, 2002 10:42 AM
To: Frank Holmes; tutor@python.org
Subject: Re: [Tutor] How do I save a dictionary to disk? (another newbie
question)


Hi Frank,

>    I am working on a simple program for recalling phone numbers. I am
using
> a dictionary to deal with variations in spelling, etc. I took the
dictionary
> out of the 'while' loop and the add_name() function works as planned....

Goody :)

> except (duh..) when I quit the program and restart it the names I have
added
> are no longer there. They exist only locally and are dumped when the
program
> ends.
>    I beat my head against this for a few hours today, went through my
books
> and cannot figure out how to store the updated dictionary.
>    I know this is probably easy stuff but I am completely stumped.

It is as long as you have a lot of spare time and use it to go through the
Python Library Reference ;) Ah i'm such a nerd.
You'll want to look at the "shelve" module, I think you'll find it perfect
for your case. Basically it's an emulation of a dictionary, but with
persistence added.
http://www.python.org/doc/current/lib/module-shelve.html

>
>    I am using python on a windows98 system.
>
> def add_name():
>      print "Enter Name"
>      n=raw_input()
>      d1[n]=m
>
> d1={'name':'1111', 'a. name':'1111', 'different name':'2222'}
> while 1:
>      print "A Greeting"
>      print "Please enter name to search"
>      print "Type 'add' to add entries or 'quit' to exit"
>      x=raw_input()
>      if x=='quit':
>           break
>      if x in d1.keys():
>           print d1[x], x
>      if x=='add':
>           add_name()
>
>
> _________________________________________________________________
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


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