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 dat…


This content originally appeared on DEV Community and was authored by Kathan Vakharia

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.

image

❗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" }

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)
  • 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 
print(retreived_dict)

#Output
#{'name': 'Gaitonde', 'location': 'Chand ?', 'side-kick': 'Bunty'}

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.


This content originally appeared on DEV Community and was authored by Kathan Vakharia


Print Share Comment Cite Upload Translate Updates
APA

Kathan Vakharia | Sciencx (2021-08-20T12:38:58+00:00) Pickling and Unpickling in Python?. Retrieved from https://www.scien.cx/2021/08/20/pickling-and-unpickling-in-python%f0%9f%a5%92/

MLA
" » Pickling and Unpickling in Python?." Kathan Vakharia | Sciencx - Friday August 20, 2021, https://www.scien.cx/2021/08/20/pickling-and-unpickling-in-python%f0%9f%a5%92/
HARVARD
Kathan Vakharia | Sciencx Friday August 20, 2021 » Pickling and Unpickling in Python?., viewed ,<https://www.scien.cx/2021/08/20/pickling-and-unpickling-in-python%f0%9f%a5%92/>
VANCOUVER
Kathan Vakharia | Sciencx - » Pickling and Unpickling in Python?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/20/pickling-and-unpickling-in-python%f0%9f%a5%92/
CHICAGO
" » Pickling and Unpickling in Python?." Kathan Vakharia | Sciencx - Accessed . https://www.scien.cx/2021/08/20/pickling-and-unpickling-in-python%f0%9f%a5%92/
IEEE
" » Pickling and Unpickling in Python?." Kathan Vakharia | Sciencx [Online]. Available: https://www.scien.cx/2021/08/20/pickling-and-unpickling-in-python%f0%9f%a5%92/. [Accessed: ]
rf:citation
» Pickling and Unpickling in Python? | Kathan Vakharia | Sciencx | https://www.scien.cx/2021/08/20/pickling-and-unpickling-in-python%f0%9f%a5%92/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.