Python

Python YAML Dump

You may have learned about the conversion of one form of data to another in programming. Across all computer languages, YAML is a user-friendly data processing technology. Although it is frequently employed as a standard for configuration files, its support for object serialization makes it a competitive alternative to JSON. It came up with the “dump” function statement to convert a simple Python dictionary to the YAML file format. We will deliberate the use of the dump() function of YAML. Before that, you must have the Python and yaml installed in your Linux shell. If not, try using the following installation instruction for YAML in the shell.

Example 01:

We start our first example of Python from the creation of a new Python file. For this, we try the simple ‘touch’ query of the Ubuntu 20.04 Linux shell to generate a Python file named “test.py”.


We prefer to use the default “text” editor since it’s more convenient to write the code within it and save it. Open the empty Python file within the text editor and start to code within it. We add the python path within this code in the first line. The code is initiated with the simple import of the “yaml” repository to use the “yaml” related functions within the code, i.e. “dump()” function. The print() function statement is here to display that we use the dictionary format first.

After this, we initialize a dictionary named “dic” with a total of 3 key-pair values in it. The next print function statement uses the “dic” dictionary to display on the Ubuntu 20.04 console. The consecutive print statement is here to display that we are going to display the converted dictionary to yaml format on the Ubuntu 20.04 Linux console. In the 7th line of code, we call the “dump” function with the yaml package that utilizes the “dic” dictionary within its parameter to convert this dictionary to the yaml file format. This converted file format is saved to the “yml” variable and the print statement at the 8th statement uses this variable to display it on the console shell. This code is completed here and we save it using the “Ctrl+S”.


Coming back to the shell terminal, we use the Python command to execute the “test.py” Python file. The output displays the simple dictionary format at the second line of output and then it displays the yaml file format of the dictionary as shown in the following image:

Example 02:

Within our first example, we showed how to convert a simple dictionary to the yaml file format. Now, we will convert a yaml file format to the dictionary format using the yaml package of the Python language. We initiate this code by importing the yaml package in this file. After this, we import the print package from the Python print class.

Along with that, we start the “yml” variable with the “=” and triple inverted commas to add the yml format data within it. It contains a total of 2 main keys with 2 internal key-pair values for each. This variable “yml” is used within the safe_load() function of yaml to convert this yaml file format to the dictionary format. This conversion result is saved to the “dic” variable and used within the pprint() function statement to display on the console screen. This program is now complete and ready for our use. So, we save this code in the same Python file and come back to the shell for execution.


After using the Python command with the name of a Python file “test.py”, we found the displayed result on our console screen. The yaml file data is displayed in the dictionary format with 2 main key-pair values holding the inner 2 key-pair values each.

Example 03:

Let’s consider this example to convert a dictionary to a YAML format and save the converted format to a YAML file. So, we commence this code by importing the YAML library followed by the “print” statement. The dictionary “dic” of a total of 3 key-pair values is initialized. The print function statement is consumed here to print out the dictionary as it is at the console via the use of the “dic” variable.

After that, we utilize the “with” statement followed by the “open” function. This open() function uses the name of a file to be created in a “read” format as its first argument, i.e. “result.yaml”. The “w” for the “write” argument is used to specify the mode of the file and writes some data in it. The dump() function of the yaml package writes the “dic” dictionary to the yaml file, i.e. result.yaml. Let’s save and implement this Python code now.


Upon the execution of the “test.py” file with the Python code, the dictionary is displayed and it is saved to the “result.yaml” file in a yaml file format.


You need to open or display the “result.yaml” file from the current working directory or use the “cat” command on the console to see the result. You can see that the dictionary is converted into YAML format and is saved to the YAML file.

Conclusion

This is about the use of the YAML package within the Ubuntu 20.4 Linux system to make use of its dump() function for several purposes. First, we tried the “dump” function to convert a Python dictionary to the simple YAML format within the same file code. After that, we learned of the use of the safe_load() function to convert the yaml format to the dictionary. In the end, we elaborated on the use of the dump() function to convert the dictionary to YAML format and add it to the YAML file.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.