cPickle
Maric Michaud
maric at aristote.info
Thu Sep 4 07:54:56 EDT 2008
Le Thursday 04 September 2008 13:08:59 Gerhard Häring, vous avez écrit :
> gopal mishra wrote:
> > I have 3 objects and want to save in one pickle file.
> >
> > I used cPickle to dump 3 objects in a pkl file
> >
> > i.e cPickle.dump(object1, fileobject, -1)
> >
> > cPickle.dump(object2, fileobject, -1)
> >
> > cPickle.dump(object3, fileobject, -1)
> >
> >
> >
> > I have changed the 3^rd object and want to save the updated 3^rd object
> > in the pickle file.
> >
> > I have to dump all the 3 objects again.
> >
> > Is there any way to dump only the 3^rd object in to the pkl file.
>
> No, there isn't. You could, of course, save each object in its own
> pickle file. Or use an object database like ZODB instead.
>
Shelve is another alternative, shipped with python and rather straightforward.
Shelve as ZODB use pickles of objects but, unlike ZODB, won't need you change
anything to your actual classes.
>>>[8]: import shelve
>>>[9]: s = shelve.open('db')
>>>[10]: a, b, c = 5, 4., ("foo", "bar")
>>>[11]: s['a'], s['b'], s['c'] = a, b, c
>>>[12]: s.close()
>>>[13]:
maric at redflag1 13:46:42:~$ file db
db: Berkeley DB (Hash, version 8, native byte-order)
maric at redflag1 13:46:44:~$ ipython
>>>[1]: import shelve
>>>[2]: s = shelve.open('db')
>>>[3]: for name, value in s.items() : print name, value
...:
b 4.0
a 5
c ('foo', 'bar')
>>>[5]: del s['b']
>>>[6]: s['c'] += ('baz',)
>>>[7]: s.sync()
>>>[8]:
maric at redflag1 13:48:12:~$ ipython
>>>[1]: import shelve
>>>[2]: s = shelve.open('db')
>>>[3]: for name, value in s.items() : print name, value
...:
a 5
c ('foo', 'bar', 'baz')
The sync method may not work on all platform, maybe you'll have to close and
re-open the db file to write to disk.
--
_____________
Maric Michaud
More information about the Python-list
mailing list