Pickle vs XML for file I/O

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Aug 2 01:51:23 EDT 2006


In <1154451307.232785.282080 at m79g2000cwm.googlegroups.com>, crystalattice
wrote:

>> What are the problems you fear when using `shelve` by the way?
>>
> The ideas I got about shelve are mostly due to this thread:
> http://tinyurl.com/lueok.  There weren't any other threads
> contradicting the information so I figured it has merit.

Main complaint seemed to be that data might be corrupted when the program
terminates "abnormal" while writing the data.  You have the very same
problem with pickle or XML serialization.  If the program gets interrupted
after only half the data is written to disk you lose information.

> Actually, not many people seem to use shelve very much; pickle is used
> more often so I decided to give it a try and see how it works for me.

They have a slightly different use case.  `pickle` stores single objects
and `shelve` is a sort of persistent dictionary that maps strings to
objects.  So if you want to save a party of characters and load them back
together than pickle a list with those characters.  If you want a database
of many characters the player can choose from than you might want to store
them in a `shelve`.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list