The pickle Module

The pickle module, shown in Example 4-11, is used to serialize datathat is, convert data to and from character strings, so that they can be stored on file or sent over a network. It's a bit slower than marshal, but it can handle class instances, shared elements, and recursive data structures, among other things.

Example 4-11. Using the pickle Module

File: pickle-example-1.py

import pickle

value = (
 "this is a string",
 [1, 2, 3, 4],
 ("more tuples", 1.0, 2.3, 4.5),
 "this is yet another string"

data = pickle.dumps(value)

# intermediate format
print type(data), len(data)

print "-"*50
print data
print "-"*50

print pickle.loads(data)

(S'this is a string'
a(S'more tuples'
S'this is yet another string'
('this is a string', [1, 2, 3, 4], ('more tuples',
1.0, 2.3, 4.5), 'this is yet another string')

On the other hand, pickle cannot handle code objects (but see the copy_reg module for a way to fix this).

By default, pickle uses a text-based format. You can also use a binary format, in which numbers and binary strings are stored in a compact binary format. The binary format usually results in smaller files. This is demonstrated in Example 4-12.

Example 4-12. Using the pickle Module in Binary Mode

File: pickle-example-2.py

import pickle
import math

value = (
 "this is a long string" * 100,
 [1.2345678, 2.3456789, 3.4567890] * 100

# text mode
data = pickle.dumps(value)
print type(data), len(data), pickle.loads(data) == value

# binary mode
data = pickle.dumps(value, 1)
print type(data), len(data), pickle.loads(data) == value

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols


Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh

Similar book on Amazon

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net