
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pickling
Python pickle module is used for serializing and de-serializing python object structures. The process to converts any kind of python objects (list, dict, etc.) into byte streams (0s and 1s) is called pickling or serialization or flattening or marshalling. We can converts the byte stream (generated through pickling) back into python objects by a process called as unpickling.
Why Pickle?: In real world sceanario, the use pickling and unpickling are widespread as they allow us to easily transfer data from one server/system to another and then store it in a file or database.
Precaution: It is advisable not to unpickle data received from an untrusted source as they may pose security threat. However, the pickle module has no way of knowing or raise alarm while pickling malicious data.
Only after importing pickle module we can do pickling and unpickling. Importing pickle can be done using the following command −
import pickle
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.