save dictionary for later use?

jay graves jaywgraves at gmail.com
Fri May 16 17:21:57 EDT 2008


On May 16, 3:24 pm, globalrev <skanem... at yahoo.se> wrote:
> On 16 Maj, 21:22, jay graves <jaywgra... at gmail.com> wrote:
> > On May 16, 2:17 pm, globalrev <skanem... at yahoo.se> wrote:
> > > i extract info from one file and put it into a dictionary.
> > > i want to save that dictionary for later use, how do i do that?
> > > might save a list of dictionaries or a list of classobjects too if
> > > there is any difference.
> > use the 'pickle' module.http://docs.python.org/lib/module-pickle.html

> pickle.dumps(mg)
> pickle.load(mg)
>
> 'dict' object has no attribute 'readline'
> dumps load(well i dont know but i get no complaint but running load
> generates that error)

It's best to post a minimal set of code that exhibits your error.
You aren't saving the output of pickle.dumps and you are using
pickle.load instead of pickle.loads.

Sample loading to and from a string which you can tuck away in a file.

>>> import pickle
>>> test = {'a':1,'b':2}
>>> picklestr = pickle.dumps(test)
>>> test2 = pickle.loads(picklestr)
>>> test == test2
True
>>>


Sample using an open file.

>>> import pickle
>>> test = {'a':1,'b':2}
>>> pfile = open('pickletest','wb')
>>> pickle.dump(test,pfile)
>>> pfile.close()
>>> pfile = open('pickletest','rb')
>>> test2 = pickle.load(pfile)
>>> pfile.close()
>>> test == test2
True
>>>

...
Jay Graves



More information about the Python-list mailing list