Pickle dump in Python

The next section will teach us how to store data using Python’s pickle format. We must first import the pickle module to do this task. The data from the object may then be saved to the file by implementing the pickle.dump() method.

Syntax

Pickle dump in Python

The pickle.dump() method accepts two parameters. The object that you intend to save is sent in as the first parameter. The second statement is the directory entity you receive after accessing the specified file in write-binary (wb) mode.

Example 01: Inserting Multiple Lines of Data in a Pickle File Using the Pickle Dump Function

In this example, we are going to look at a simple example in which we will be using a pickle.dump() method to a file. The first step is to import the pickle module in Python to use its methods. In the next line, an input prompt is used to ask the user to enter the number of digits the user wishes to enter. It is stored in the variable “num”. An empty list is also initialized as “data”. Then a for loop is run for the number of data times. In each loop, the program asks the user to input the data. This data is appended to the “data” list.

When all of the data is appended and the loop ends, a file is opened in write-binary (wb) mode and is stored in the variable “file”. In the next line, pickle.dump() method is implemented on the given file with the “data”. Finally, the file is closed.

Pickle dump in Python

At the output terminal, we can see the input prompts that the program asked the user to input. In this example, the quantity of data was 2. One was number 12 and the other was number 15. If we open the file, we would be able to see the data is written in that file.

Pickle dump in Python

We can see three files for the following program. One is the file in which the code is written. The main file is where the code is executed. The “info” file is the binary file created by pickle.dump() in which we dumped the data.

Pickle dump in Python

Now, we will see how we can retrieve the pickle data. To retrieve the pickle data back, you must use the pickle.load() method. When viewing a file in read-binary (rb) mode, the directory contents can be accessed.

Pickle dump in Python

As the pickle.load() is also a method of the pickle module, we need to import this module first to use it. In the next line, the “info” file is opened in rb mode. Then the pickle.load() method is called on the file object and data is stored in the variable named “data”. Then a for loop is traversed through the data to print the data at the output terminal, as shown in the following screenshot:

Pickle dump in Python

Example 02: Inserting Multiple Lists of Data in a Pickle File Using the Pickle Dump Function

Another sample of pickling and unpickling data is shown below:

Pickle dump in Python

In the previous code, two dictionaries are created for two persons namely, John and Logan, with their respective names and age. The “list1” dictionary contains the attributes of both persons. Then “info” file is opened in append-mode (ab). Pickle.dump() is applied to the file object which appends the “list1” dictionary/data to it. Finally, the file is closed.

Pickle dump in Python

Pickle dump in Python

In the previous code, we will be using the pickle.load() method to unpickle the data that we just pickled in the “info” file. The file is opened in rb mode and pickle.load() method is applied to the file object. The returned data is stored in the variable named “list1”. The for loop is then used to show the whole contents of “list1” at the secondary terminals, as seen in the following image:

Pickle dump in Python

Example 03: Inserting Multiple Lists of Data in Without a Pickle File Using the Pickle Dump Function and Then Loading It in the Console

Now, let’s look at several examples of pickling and unpickling data without using a file. Also, this time we will be using pickle.dumps() and pickle.loads() methods. The only difference is that the latter method creates and reads the file containing the serialization result, whereas the dumps() and loads() method operates on a string rather than a file and returns a string.

Pickle dump in Python

In the previous code, two different dictionaries are created containing respective details about John and Logan. Then a primary dictionary “list1” is created and these two separate dictionaries are added to it. In the next line, the pickle .dumps() method is used to dump the data as a string in the new object, “a”. In the next line, the dumped data is unpickled using a pickle.loads() method and stores the data in a new variable, “Entry”. The result tab then presents the unpickled data as seen in the image beneath.

Pickle dump in Python

Example 04: Inserting Data in a Pickle File Using the Class Structure to Pass Data Into the Pickle Dump Function

In this example, we will be using classes to understand the working of pickle and unpickle methods. In the following code, a class named, “datalist”, is created with a function load_data(). An integer and a string are defined in objects, “a” and “b” respectively. Then outside the class, an object of datalist() is created with the name “obj”, which contains the data. Then this object is pickled and stored in a new variable “data” using pickle.dumps() method. To unpickle the data, pickle.loads() method is implemented on “data”.

Pickle dump in Python

Finally, the returned data from pickle.loads() is displayed at the output terminal in the form of the dictionary, as shown in the following screenshot:

Pickle dump in Python

Example 05: Inserting a Long String in a Pickle File Using the Compress Function Along With the Pickle Dump Function

The next case will demonstrate how to compress pickled items. You may use the bz2 module from the standard Python library to compress a pickled string using bzip2.

Pickle dump in Python

In this code, the first-string type data is initialized and stored in a variable named “data”. In the next line, this data is pickled using a pickle. dumps() to the object named “dump”. Then this object is compressed using the bz2.compress() method and stored in object “dump2”. Here, “dump” contains the uncompressed data of string while the “dump2” object contains the compressed data. We can prove this by finding the length of both objects using the len() command.

Pickle dump in Python

Conclusion

I hope through this tutorial, you now understand how to use Python’s pickle module to convert an object hierarchy to a stream of bytes that can be stored to disc or sent over a network as well as Python’s deserialization method. You are now well-equipped to persist objects using the Python pickle package.

What is pickle dump () in Python?

Python Pickle dump dump() function to store the object data to the file. pickle. dump() function takes 3 arguments. The first argument is the object that you want to store. The second argument is the file object you get by opening the desired file in write-binary (wb) mode.

Is dump () used for pickling?

Once the file is opened for writing, you can use pickle. dump() , which takes two arguments: the object you want to pickle and the file to which the object has to be saved.

How do I dump a pickle file?

Write a pickle file dump function, which takes two arguments: the first one is the object, and the second argument is a file object returned by the open function. Note here the mode of the open function is 'wb' which indicates write binary file.

What is the use of dump () and load () function?

Conversion of data at the time of reading and writing is required. dump(object , file_handler) - used to write any object to the binary file. Object=load(file_handler) - used to read object from the binary file.