DEV Community

Cover image for Pickling and Unpickling in Python
Kathan Vakharia
Kathan Vakharia

Posted on

Pickling and Unpickling in Python

What is pickling?

In Layman's terms, Pickling is a process to convert your python objects into a file.

Technically speaking, it is a way to convert your well-known python objects like lists, sets, tuples, dictionaries, strings, pandas dataframes,... to a character stream. And that character stream contains all the information required to reconstruct that python object in another script.


Note Pickling Compression They both are completely different!

For all those programming nerds out there, The process of converting an object to a file is also known as Serialization or Marshalling or Flattening.

Retrieving that object back from the file is known as DeSerialization or Unmarshalling or Unflattening.

Python provides the pickle module for performing serialization/deserialization.

Let's say you want to pickle a dictionary into a file,

  • First of all, we need to import pickle ,
import pickle
#A Sacred Dictionary 
sacred_dict = {"name":"Gaitonde", "location":"Chand " ,
              "side-kick":"Bunty" }
Enter fullscreen mode Exit fullscreen mode

Let's see how to use it.
Here are the simple steps,

  1. Open/Create the file in which you want to store this dictionary.
  2. Call pickle.dump() by passing the dictionary and file object.
w  => Write mode
b  => Binary mode
wb => Write in binary mode
with open("sacred.pkl", "wb") as f:
        pickle.dump(sacred_dict, f)
Enter fullscreen mode Exit fullscreen mode
  • On executing the last snippet "sacred.pkl" file will be created if not present which then will be filled with sacred_dict in form of the character stream.
  • It will be placed in the current working directory. You can also pass the exact file path if you want it to be stored somewhere else!

That's it, You have stored your dictionary in a file! Easy Peasy

Now Let's see how to unpickle or retreive that dictionary back.

The .pkl extension is just a convention that helps us identify it as a pickle file.

To retrieve the object back, we have to use the pickle.load() method passing the file object of the pickled file,

r  => Read mode
b  => Binary mode
rb => Read in Binary mode
with open("sacred.pkl", "rb") as f:
        retreived_dict = pickle.load(f)

#Let's print retreived_dict to confirm 

#{'name': 'Gaitonde', 'location': 'Chand ', 'side-kick': 'Bunty'}
Enter fullscreen mode Exit fullscreen mode

Important Points about while Pickling and Unpickling

  • It is protocol specific to python, don't try to unpickle a file pickled in python in some other programming language. Thus, cross-language compatibility is not guaranteed.
  • Moreover, Unpickling a file that was pickled in a different version of Python may not always work properly, so you have to make sure that you're using the same version and perform an update if necessary.
  • The pickle module is not secure. Only unpickle data you trust.

That doesn't mean, you should not use pickle module. Just make sure you trust the source of that pickle file.

Top comments (0)