



(10 ratings)
>>> import cPickle as pickleNow that we've imported the module, let's take a look at the pickle interface. The pickle module provides the following function pairs: dumps(object) returns a string containing an object in pickle format; loads(string) returns the object contained in the pickle string; dump(object, file) writes the object to the file, which may be an actual physical file, but could also be any file-like object having a write() method that accepts a single string argument; load(file) returns the object contained in the pickle file.
By default, dumps() and dump() create pickles using a printable ASCII representation. Both functions have a final, optional argument that, if True, specifies that pickles will be created using a faster and smaller binary representation. The loads() and load() functions automatically detect whether a pickle is in the binary or text format.
Listing 1 shows an interactive session using the dumps() and loads() functions just described:
Listing 1. Illustration of dumps() and loads()
Welcome To PyCrust 0.7.2 - The Flakiest Python ShellNotice that the text pickle format isn't too difficult to decipher. In fact, the conventions used are all documented in the pickle module. We should also point out that with the simple objects used in our example, there wasn't much space efficiency gained by using the binary pickle format. However, in a real system with complex objects, you will see a noticable size and speed improvement with the binary format.
Sponsored by Orbtech - Your source for Python programming expertise.
Python 2.2.1 (#1, Aug 27 2002, 10:22:32)
[GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux-i386
Type "copyright", "credits" or "license" for more information.
>>> import cPickle as pickle
>>> t1 = ('this is a string', 42, [1, 2, 3], None)
>>> t1
('this is a string', 42, [1, 2, 3], None)
>>> p1 = pickle.dumps(t1)
>>> p1
"(S'this is a string'\nI42\n(lp1\nI1\naI2\naI3\naNtp2\n."
>>> print p1
(S'this is a string'
I42
(lp1
I1
aI2
aI3
aNtp2
.
>>> t2 = pickle.loads(p1)
>>> t2
('this is a string', 42, [1, 2, 3], None)
>>> p2 = pickle.dumps(t1, True)
>>> p2
'(U\x10this is a stringK*]q\x01(K\x01K\x02K\x03eNtq\x02.'
>>> t3 = pickle.loads(p2)
>>> t3
('this is a string', 42, [1, 2, 3], None)
Next we'll look at some examples using dump() and load(), which work with files and file-like objects. These functions operate much like the dumps() and loads() that we just looked at, with one additional capability -- the dump() function allows you to dump several objects to the same file, one after the other. Subsequent calls to load() will retrieve the objects in the same order. Listing 2 shows this capability in action:
Listing 2. Example of dump() and load()
>>> a1 = 'apple'
>>> b1 = {1: 'One', 2: 'Two', 3: 'Three'}
>>> c1 = ['fee', 'fie', 'foe', 'fum']
>>> f1 = file('temp.pkl', 'wb')
>>> pickle.dump(a1, f1, True)
>>> pickle.dump(b1, f1, True)
>>> pickle.dump(c1, f1, True)
>>> f1.close()
>>> f2 = file('temp.pkl', 'rb')
>>> a2 = pickle.load(f2)
>>> a2
'apple'
>>> b2 = pickle.load(f2)
>>> b2
{1: 'One', 2: 'Two', 3: 'Three'}
>>> c2 = pickle.load(f2)
>>> c2
['fee', 'fie', 'foe', 'fum']
>>> f2.close()
20 Random Tutorials from the same category :
Floating Point Arithmetic: Issues and Limitations
Functions and Strings -Python
Organization in Python
Why is Python popular with Linux users?
Defining regular expressions in Phyton
Python vs. Perl
Simple Statements In Python
Python with other Languages
Interactive Input Editing and History Substitution
Classes and objects - Python
Modules
Using the Python Interpreter
Control Structures In Python
Google Sitemaps - Python
Python 101 -- Introduction to Python
A peck of pickled Python
Python Issues and some Tips
Python and Java - A Side by Side Comparison
The Other Scripting Language that Starts with "P"
Modules in Python













