19
loading...
This website collects cookies to deliver better user experience
In Layman's terms, Pickling is a process to convert your python objects into a file.
❗Note Pickling ≠ Compression → They both are completely different!
pickle
module for performing serialization/deserialization.pickle
,
import pickle
#A Sacred Dictionary 😌
sacred_dict = {"name":"Gaitonde", "location":"Chand 🌙" ,
"side-kick":"Bunty" }
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)
sacred_dict
in form of the character stream.The .pkl extension is just a convention that helps us identify it as a pickle file.
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'}
That doesn't mean, you should not use pickle
module. Just make sure you trust the source of that pickle file.