Python Tutorial

Python Pickling Explained

What is Pickling in Python?

The role of the Python pickle is to serialize or deserialize an object structure.  Pickling an object in Python enables it to be stored on a disc. Pickle works by first "serializing" the item before writing it to file. 

Pickling is a Python function that turns a character stream from a list, dict, or other Python object. This character stream consists of all of the data required to reconstruct the object in another Python script.

Example:

import pickle

def storeData():

    # initializing data to be stored in db

    Omish = {'key' : 'Omish', 'name' : 'Omish Bhatnagar',

    'age' : 21, 'pay' : 40000}

    Janardan = {'key' : 'Janardan', 'name' : 'Janardan Bhatnagar',

    'age' : 50, 'pay' : 50000}

    # database

    db = {}

    db['Omish'] = Omish

    db['Janardan'] = Janardan  

    # Its important to use binary mode

    dbfile = open('examplePickle', 'ab')  

    # source, destination

    pickle.dump(db, dbfile)                     

    dbfile.close()

def loadData():

    # for reading also binary mode is important

    dbfile = open('examplePickle', 'rb')     

    db = pickle.load(dbfile)

    for keys in db:

        print(keys, '=>', db[keys])

    dbfile.close()

if __name__ == '__main__':

    storeData()

    loadData()

Output:

OmishBhatnagar-Inspiron-3542:~/Documents/Python-Programs$ python P60_PickleModule.py
Omish => {'age': 21,  'name': 'Omish Bhatnagar',  'key': 'Omish',  'pay': 40000}
Janardan => {'age': 50,  'name': 'Janardan Bhatnagar',  'key': 'Janardan',  'pay': 50000}

Protocol Formats of Python Pickle Module

The pickle module is Python-specific, as previously stated, and the outcome of a pickling operation can only be accessed by another Python application. Even if you're not using Python, you should be aware that the pickle module has grown over time.

This implies that if you pickled an object with a certain Python version, you might not be able to unpickle it with an older version. The protocol version you selected for the pickling procedure determines compatibility.

The Python pickle module supports six distinct protocols at the moment. For unpickling, the Python interpreter must be more current than the protocol version

Storing Data with Python Pickles

Pickling lets you save a Python object to your hard disc as a binary file. You can end your Python session once you pickle your object, reset your machine if necessary, and then reload your object into Python.

You can save your pickle file to Google Drive, DropBox, or a regular old USB drive. Furthermore, you can share it with fellow developers or other teams through email.

A word of caution: Don't load pickles you're not sure about. Malicious persons can build malicious pickles that can cause your computer to execute unexpected code (SQL injection, password brute-forcing, etc). Bad pickles should be avoided at all costs.

Example:

import pickle

obj = {'x':[4,2,1.5], 'y':[3,[10],1], 'f':True, 's':False}

Use pickle.dump to save a pickle.
Did you find this article helpful?