Get yourself into a Python cPickle
Published: 15 Aug 2002 11:44 BST

There are many reasons to do this, and most languages today provide some prepackaged approach. Python is no different. I'll start with a little background information on serialisation. Then, I'll walk through an example that shows how to use cPickle to serialise and deserialise list values.
Your basic pickle
Serialisation, also called pickling or flattening, converts structured data into a data stream format. Essentially, this means that structures such as lists, tuples, functions, and classes are preserved using ASCII characters between data values. The pickle data format is standardised, so strings serialised with pickle can be deserialised with cPickle and vice versa.
The main difference between cPickle and pickle is performance. The cPickle module is many times faster to execute because it's written in C and because its methods are functions instead of classes. While this improves performance, it also means that the cPickle methods can't be extended or customised, whereas pickle classes can.
Serialisation is useful in a number of ways. It can be a time and resource saver when used on data that will be transmitted, encrypted, or stored in a database. Information is serialised in Python and then processed. When the data is retrieved, it is deserialised and used.
For additional information on cPickle and pickle, refer to the Python online reference manual's section "3.14 pickle--Python object serialisation."
Serving up condiments: A cPickle example
Now let's look at a simple example that demonstrates basic usage of the cPickle module.
First, the condiments.py script informs Python that we'll be using the cPickle module:
import cPickle
Next, I define the object I want to serialise and store. In this case, it's a list of condiments I've got in the fridge:
inFridge = ["ketchup", "mustard", "relish"]
print inFridge
That print statement's output will display the following:
['ketchup', 'mustard', 'relish']
I want to save my results in a file called fridge.txt, so the script creates a file handler and opens the file for writing:
FILE = open("fridge.txt", 'w')
Now the magic happens. I call the cPickle command, dump, to pickle my data and dump the results to the file:
cPickle.dump(inFridge, FILE)
I'm finished for now, so the script closes the file:
FILE.close()
I now have a file, fridge.txt, that contains the following:
(lp1
S'ketchup'
p2
aS'mustard'
p3
aS'relish'
p4
a.
The pickle and cPickle modules have an option to save the information in a binary format; however, I've used the default ASCII because it is human-readable.





