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?
- Store the List of Data in a File
- Store Dictionary Data in a File
- Store User-Input Data in a File
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
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:
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:
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()”:
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:
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:
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:
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.