Search notes:

Python standard library: pickle

The pickle module allows to serialize arbitrarily complex objects.
It should be noted that the pickle module is insecure if used with data from an unstrusted source.

Dump an object to a file and load it again

import pickle

obj = [
   {'col 1': 'one'  , 'col 2': 1, 'col 3': 'foo'},
   {'col 1': 'two'  , 'col 2': 2, 'col 3': 'bar'},
   {'col 1': 'three', 'col 2': 3, 'col 3': 'baz'},
]

pickle.dump(obj, open('obj.pickled', 'wb'))
import pickle

obj = pickle.load(open('obj.pickled', 'rb'))

print(obj)

Members of the pickle module

ADDITEMS bytes object
APPEND bytes object
APPENDS bytes object
BINBYTES bytes object
BINBYTES8 bytes object
BINFLOAT bytes object
BINGET bytes object
BININT bytes object
BININT1 bytes object
BININT2 bytes object
BINPERSID bytes object
BINPUT bytes object
BINSTRING bytes object
BINUNICODE bytes object
BINUNICODE8 bytes object
BUILD bytes object
BYTEARRAY8 bytes object
bytes_types tuple object
codecs Module
compatible_formats list object
_compat_pickle Module
decode_long() Function
DEFAULT_PROTOCOL int object
DICT bytes object
dispatch_table dict object
_dump() Function
dump()
_dumps() Function
dumps() Returns the pickled representation of the object as a bytes object.
DUP bytes object
EMPTY_DICT bytes object
EMPTY_LIST bytes object
EMPTY_SET bytes object
EMPTY_TUPLE bytes object
encode_long() Function
EXT1 bytes object
EXT2 bytes object
EXT4 bytes object
_extension_cache dict object
_extension_registry dict object
FALSE bytes object
FLOAT bytes object
format_version str object
FRAME bytes object
_Framer pickle._Framer class
FROZENSET bytes object
FunctionType function class
GET bytes object
_getattribute() Function
GLOBAL bytes object
_HAVE_PICKLE_BUFFER bool object
HIGHEST_PROTOCOL int object
INST bytes object
INT bytes object
_inverted_registry dict object
io Module
islice itertools.islice class
LIST bytes object
_load() Function
load()
_loads() Function
loads()
LONG bytes object
LONG1 bytes object
LONG4 bytes object
LONG_BINGET bytes object
LONG_BINPUT bytes object
MARK bytes object
maxsize int object
MEMOIZE bytes object
NEWFALSE bytes object
NEWOBJ bytes object
NEWOBJ_EX bytes object
NEWTRUE bytes object
NEXT_BUFFER bytes object
NONE bytes object
OBJ bytes object
pack()
partial functools.partial class
PERSID bytes object
PickleBuffer pickle.PickleBuffer class
PickleError _pickle.PickleError class
Pickler _pickle.Pickler class
_Pickler pickle._Pickler class
PicklingError _pickle.PicklingError class
POP bytes object
POP_MARK bytes object
PROTO bytes object
PUT bytes object
PyStringMap NoneType object
re Module
READONLY_BUFFER bytes object
REDUCE bytes object
SETITEM bytes object
SETITEMS bytes object
SHORT_BINBYTES bytes object
SHORT_BINSTRING bytes object
SHORT_BINUNICODE bytes object
STACK_GLOBAL bytes object
STOP bytes object
_Stop pickle._Stop class
STRING bytes object
sys Module
_test() Function
TRUE bytes object
TUPLE bytes object
TUPLE1 bytes object
TUPLE2 bytes object
TUPLE3 bytes object
_tuplesize2code list object
_Unframer pickle._Unframer class
UNICODE bytes object
unpack()
Unpickler _pickle.Unpickler class
_Unpickler pickle._Unpickler class
UnpicklingError _pickle.UnpicklingError class
whichmodule() Function

See also

The standard library modules
The two pandas methods read_pickle() (which pickles a DataFrame) and to_pickle (which reads a pickled object).

Index