Definition of Python Dump
The Python supports json package which is built in to execute the script file which contains the textual programming code that can be used to transfer and store the data. It can be done by calling the json package in python. The dump function in json supports the code scripted in key-value pairs similar to the python dictionary that is within curly brackets. The dumps function is mainly used when we wanted to store and transfer python objects and json package allows us to perform the operation efficiently.
Syntax
The python dump function is used by importing packages like json and pickle in python and the basic syntax for both the functions is,
json.dump(object, skipkeys=False, ensure_ascii=True, indent=None, allow_nan=True, number_mode = None, datetime_mode = None, separators=None)
pickle.dump(Object, Destination, pickle_protocol=None, )
- json.dump represents the function that encodes and stores the python object value into json value.
- object is the filename of the input data or a value which we are passing through the dump function.
- skipkeys is a parameter where we will declare Boolean values whether we wanted to skip the dictionary keys which are invalid.
- ensure_ascii it is a parameter where we will declare the Boolean values to ensure the output should contain ASCII values or not.
- indent is a parameter where the json code is printed with neat indentation.
- allow_nan is also a Boolean parameter which is used to allow null values.
- number_mode & datetime_mode allows us to handle the type of behaviors we handle inside the function and datetime mode allows to handle to format of data and time instances.
- Separators are used for two purposes where we can either declare one or two values to the parameter. The value we give first denotes the separation of a key value pair with other key value pair. 2nd value we give denotes the symbol which separated keys from its values.
For pickle package,
- Object is the python object we have created to be pickled
- Destination is the file or data where the pickled python objected is written
- Pickle_protocol refers to the version of the pickle protocol by default it assigns to the python version.
How does Python Dump Function Work?
Let us discuss a basic example for understanding how the json dump function works.
Example #1
Code:
import json
# python dictionary
dict_pets ={
"Dog": {
"Species": "cocker spaniel",
"country": "United Kingdom"
},
"Cat": {
"Species": "British Shorthair",
"country": "United Kingdom"
},
"Hamster": {
"Species": "golden hamster ",
"country": "Turkey"
}
}
## Converting output to json format
pets_data = open("pet_data.json", "w")
json.dump(dict_pets, pets_data)
Output:
In this example, we have created a python dictionary with three key-value pairs and we have converted the python dictionary to json file format using the json package. Then the dictionary variable is declared into the json dump function to get the output in json format. Here when we use the json.dump function we need to have two positional arguments (dict_pets and pets_data) because dict_pets represent the python object that we want to serialize and pets_data is the file where the json output is stored or written.
Example #2
In this example, we’ll discuss the package called pickle in python which helps us in the serialization of the python object.
Code:
import pickle
# python dictionary
dict_pets ={
"Dog": {
"Species": "cocker spaniel",
"country": "United Kingdom"
},
"Cat": {
"Species": "British Shorthair",
"country": "United Kingdom"
},
"Hamster": {
"Species": "golden hamster ",
"country": "Turkey"
}
}
## Serializing output using pickle
pets_data = open("pet_data.pickle", "wb")
pickle.dump(dict_pets, pets_data)
Output:
Similar to json we have dumped the python object using the pickle package which is s very specific library in python where we can serialize the python object by using pickle.dump() function. We have declared three python dictionaries and tried to dump the dictionary object in pickle format. This method is primarily used for objects written in python code and codes from other languages is cannot be serialized or dumped using the pickle function. “wb” is parameter we have used in pickle function which is open to writing mode and binary mode.
Example #3
Let’s discuss another example where we use the json dumps() function which is similar to the dump() function but the dumps() function allows us to convert the python dictionary object to a string file in json format.
Code:
import json
import json
# python dictionary
dict_pets ={
"Dog": {
"Species": "cocker spaniel",
"country": "United Kingdom"
},
"Cat": {
"Species": "British Shorthair",
"country": "United Kingdom"
},
"Hamster": {
"Species": "golden hamster ",
"country": "Turkey"
}
}
## Converting output to json format
json_dict = json.dumps(dict_pets)
print(json_dict)
Output:
Similar to the 1st example we have created the python dictionary with the same three key-value pairs and here we have passed only one positional argument during the dumps() function unlike json.dump() where two positional arguments required.
Since we are converting the python object to json string format we only require the object variable.
Example #4
In this example, we have used the allow_nan parameter which is one of the parameters we have discussed earlier so we’ll try to implement the parameter with the python dictionary that has a nan value.
Code:
import json
import json
# python dictionary
dict_pets ={
"Dog": {
"Species": "cocker spaniel",
"country": "United Kingdom"
},
"Cat": {
"Species": "British Shorthair",
"country": "United Kingdom"
},
"Hamster": {
"Species": "golden hamster ",
"country": "Turkey"
}
}
## Converting output to json format
json_dict = json.dumps(dict_pets)
print(json_dict)
Output:
When we declare the allow_nan parameter as True
import json
# python dictionary which should be dumped
dict_pets ={
"Dog": {
"Species": "cocker spaniel",
"country": "United Kingdom",
"life expectency": 20
},
"Hamster": {
"Species": "golden hamster",
"country": "Turkey",
"life expectency": float("nan")
}
}
## Converting output to json format
pets_data = open("pet_data.json", "w")
json.dump(dict_pets, pets_data, allow_nan=True)
Output:
we can see from two codes that when we set the allow_nan parameter as True when our object has Nan values we can dump the object to json output without any problem.
Conclusion
In this article, we have discussed the python dump function in detail using various examples to get a clear understanding on the json dump function and its uses. We have also discussed pickle package and dumps() function along with the examples and we have discussed the usage of allow_nan parameters with an example. I hope this article helps.
Recommended Articles
This is a guide to Python Dump. Here we also discuss the definition and how does the python dump function work along with different examples and its code implementation. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses