What is Pickling:
Pickle examples:
Below is a simple program on how to pickle a list:
Pickle a simple list: Pickle_list1.py
import pickle
mylist = ['a', 'b', 'c', 'd']
with open('datafile.txt', 'wb') as fh:
pickle.dump(mylist, fh)
In the above code, list – “mylist” contains four elements (‘a’, ‘b’, ‘c’, ‘d’). We open the file in “wb” mode instead of “w” as all the operations are done using bytes in the current working directory. A new file named “datafile.txt” is created, which converts the mylist data in the byte stream.
Unpickle a simple list: unpickle_list1.py
import pickle
pickle_off = open ("datafile.txt", "rb")
emp = pickle.load(pickle_off)
print(emp)
Output: On running above scripts, you can see your mylist data again as output.
['a', 'b', 'c', 'd']
Pickle a simple dictionary −
import pickle
EmpID = {1:"Zack",2:"53050",3:"IT",4:"38",5:"Flipkart"}
pickling_on = open("EmpID.pickle","wb")
pickle.dump(EmpID, pickling_on)
pickling_on.close()
Unpickle a dictionary −
import pickle
pickle_off = open("EmpID.pickle", 'rb')
EmpID = pickle.load(pickle_off)
print(EmpID)
On running above script(unpickle) we get our dictionary back as we initialized earlier. Also, please note because we are reading bytes here, we have used “rb” instead of “r”.
Output
{1: 'Zack', 2: '53050', 3: 'IT', 4: '38', 5: 'Flipkart'}
Pickle Exceptions
Below are some of the common exceptions raised while dealing with pickle module −
- Pickle.PicklingError: If the pickle object doesn’t support pickling, this exception is raised.
- Pickle.UnpicklingError: In case the file contains bad or corrupted data.
- EOFError: In case the end of file is detected, this exception is raised.
Prons:
- Comes handy to save complicated data.
- Easy to use, lighter and doesn’t require several lines of code.
- The pickled file generated is not easily readable and thus provide some security.
Cons:
- Languages other than python may not able to reconstruct pickled python objects.
- Risk of unpickling data from malicious sources.
No comments:
Post a Comment