Python

Python Pickle Dump

In Python, the “pickle” module is utilized to save and load any Python object on disk. This module does this by converting the object into a stream of characters that can be used to recreate the object in another script. The pickle is not secure, we should only use it with data from trusted sources. To dump or write the data into a pickle file the “pickle.dump()” method is used in Python.

This tutorial will present you with a comprehensive guide on Python pickle dump utilizing numerous examples via the below contents:

What is the “pickle.dump()” Method in Python?

In Python, the “pickle.dump()” method is utilized to serialize an object and write it to the file.

Syntax

pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)

Parameters

In the above syntax:

  • The “obj” parameter specifies the object we want to store.
  • The “file” parameter indicates the file object we want to write the pickled representation of.
  • The “protocol” parameter specifies the protocol version to use for pickling.
  • The “fix_imports” parameter if true will automatically convert the Python 2 names to Python 3 names.
  • The “buffer_callback” parameter is a function that will be called with a buffer containing pickled data.

Return Value

None

Example 1: Using “pickle.dump()” Method to Store the List of Data in a File

In this example code, we first imported the “pickle” and declared the list of data. After that, the “open()” function is used to open the file “new.pickle” in writing binary mode. Lastly, the “pickle.dump()” method dumps the list of data to the specified file:

import pickle

data = [21, 32, 43, 54, 15]

with open('new.pickle', 'wb') as f:

  pickle.dump(data, f)

The following pickle file has been created:

To read the content of the file, we need to deserialize the file using the “pickle.load()” method. Here is an example code that will achieve this:

import pickle

with open('new.pickle', 'rb') as f:

  print(pickle.load(f))

The pickle file data content has been loaded from the file:

Example 2: Using “pickle.dump()” Method to Store Dictionary Data in a File

We can also use the “pickle.dump()” method to store the dictionary data in a file. First, we need to import the “pickle” and define the dictionary data. After that, we need to open the file in writing binary mode and dump the dictionary data to the file using the “pickle.dump()”:

import pickle

dict1 = {'Name': ['Joseph', 'Anna', 'Lily', 'Henry'], 'Age':[23, 32, 15, 19], 'Height': [5.4, 5.2, 4.7, 4.8]}

with open('new.pickle', 'wb') as f:

  pickle.dump(dict1, f)

The below snippet shows the creation of a new pickle file:

To read or deserialize the dump data of the newly created file, the “pickle.load()” method is used in Python. The following code uses the for loop and deserializes the dump data:

import pickle
with open('new.pickle', 'rb') as f:
    d1 = pickle.load(f)
    for i in d1:
        print(i, ':', d1[i])

The pickle data has been retrieved successfully:

Example 3: Using “pickle.dump()” Method to Store User-Input Data in a File

We can also dump the user-input data to the file using the “pickle.dump()” method. First, we take the total quantity of data we need to input from the user and store it in a variable. Next, an empty list is created and the for loop is iterated over the input range as well as accepting the multiple data. The append() method is utilized to append the user data to an empty list. Lastly, we open the file in writing binary mode and dump the user file data into the file using the “pickle.dump()” method:

import pickle
data1 = int(input('Enter the Data Quantity: '))
data = []
for i in range(data1):
    d1 = input('Enter data '+str(i)+' : ')
    data.append(d1)

with open('new.pickle', 'wb') as f:
    pickle.dump(data, f)

The user-input file data has been dumped into the file:

We can also deserialize the data by using the “pickle.load()” method in Python:

import pickle

with open('new.pickle', 'rb') as f:

  print(pickle.load(f))

Previously dumped data have been retrieved successfully:

Conclusion

The “pickle.dump()” method in Python is utilized to serialize an object and write/dump the list, dictionary, and other data to the file. We can declare the single or multiple data as a list, or dictionary and write them into a pickle file. The pickle.load() on the other hand is used to deserialize the pickle data and load it into Python. This guide offered a comprehensive tutorial on Python pickle dump via multiple examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.