Python Pickle Module

  • Python’s pickle module is a serialization (converting the object into streams of bytes) library which is used to serialize the python objects.That means it can be used to encode and decode your functions, classes, dict, list, string etc,.
  • Two important methods that pickle module provides are `pickle.dumps` and `pickle.loads`.

What is pickle in python used for ?

  • It is used for serializing and de-serializing a Python object structure. Any object in python can be pickled so that it can be saved on disk.
  • What pickle does “serialises” the object before writing the file. Pickling is a way to convert a python object (list, dict, etc.) into a character stream.

Pickle dump Example:

# Save a dictionary into a pickle file.
import pickle    
wikitechy_tutorial = { "Technology1": "python", "Technology2": "java" }    
pickle.dump( wikitechy_tutorial, open( "save.p", "wb" ) )

Pickle Load Example:

# Load the dictionary back from the pickle file.
import pickle    
wikitechy_tutorial = pickle.load( open( "save.p", "rb" ) )
# wikitechy_tutorial is now { "Technology1": "python", "Technology2": "java" }
what is python pickle module

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,