Pickle & Un-Pickle in Python

Youtube Live session on Tkinter

Pickle and Un-Pickle Pickle is a process to serialize ( or de-serialize ) Python object to binary protocol.
This process of serialization and di-serialization is also known as pickling and un-pickling. In the old format it is also known as marshalling and flattening.

Purpose of Pickling

By doing pickling we can transport or store the object for further use. We get a binary file to store or share with others.


Pickling is not universal format to store data. The binary file can’t be used in other languages like we use JSON, CSV, XML or other formats. This works within Python only. So we can open and use files within Python environment only.

Pickling does not create human readable format and the binary file can be infected by malicious codes. So we must open pickled files from trusted sources only.

If pickling is done using one version then un-pickling may not work in other versions.

What can be pickled

Here is a list of objects which can be pickled ( or un-pickled).
  • Booleans : True or False , None
  • Strings , bytes and byte arrays
  • Integers , float , complex numbers
  • Tuple, list , sets and dictionary
  • Functions defined at top level ( not lambda )
  • Built in functions at top level
  • Classes defined at top level

Pickle verses JSON

JSON (JavaScript Object Notation ) is a standard language for data exchange between different processes. Let us see the difference between JSON and Pickle.


JSON is human readable format , Pickle is binary format so not human readable.


JSON is acceptable format over various platforms. However other languages may not support Pickle, so this can’t be used as a standard data exchange format across the platforms.


JSON formatted data does not in itself create any vulnerability. However the binary data may content malicious codes so we must trust the source while un-pickling.

JSON because of its standard format may not offer python specific classes. As pickle supports large type of Python objects so can be handy for specific uses.

pickle.load() & pickle.dump()

pickle.load(file) : Uses the open file object and un-pickle the binary data.
pickle.dump(py_obj,file) : Uses the py_obj object to pickle and store in file.

How to pickle

Here are two examples, one is how to Pickle one simple dictionary and other one is one how to un-pickle the dictionary.
import pickle
fob = open('my_pickle','wb') # file handling object is created
pickle.dump(my_dict,fob) # generated the Pickle
fob.close() # file object is closed 
In above code we created one file object ( fob ) to handle the file my_file ( without any extension )
my_dict1=pickle.load(fob) # reading the Pickle
{'NAME': ['Ravi', 'Raju', 'Alex'], 'ID': [1, 2, 3],
 'MATH': [30, 40, 50], 'ENGLISH': [20, 30, 40]}
We can use any path along with the file name to create the file in specific location in our system.
fob = open('D:\my_data\my_pickle','wb')
pickle.dumps(py_obj) : Instead of writing to file , it returns object obj as a bytes object.
import pickle
obj_bytes=pickle.dumps(my_dict) # generated the Pickle
pickle.loads(obj): takes the bytes object as input and returns the reconstituted object
{'NAME': ['Ravi', 'Raju', 'Alex'], 'ID': [1, 2, 3], 
	'MATH': [30, 40, 50], 'ENGLISH': [20, 30, 40]}

Create a pickle ( and un-pickle ) by using data from MySQL database table.
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer