serialization

最後更新: 2020-12-11

Python object serialization

Doc

http://docs.python.org/2/library/pickle.html

 


pickle

 

The pickle module implements an algorithm for turning an arbitrary Python object into a series of bytes.

Python has a more primitive serialization module called marshal,

but in general pickle should always be the preferred way to serialize Python objects.

The module shelve provides a simple interface to pickle and unpickle objects on DBM-style database files.

* printable ASCII representation (character stream)

* XDR

# Save a dictionary into a pickle file.

import pickle
favorite_color = { "lion": "yellow", "kitty": "red" }
pickle.dump( favorite_color, open( "save.p", "wb" ) )

# Load the dictionary back from the pickle file.

import pickle
favorite_color = pickle.load( open( "save.p", "rb" ) )

 


cPickle

 

# up to 1000 times faster.

import cPickle as pickle

Creative Commons license icon Creative Commons license icon