Python

How to Read YAML File in Python

The full form of YAML is Yet Another Mark-up Language. This file format is very popular now to store serialized data that is human-readable. It is mainly used for configuration files, but it can be used for other purposes also. Different types of scalar data such as number, string, etc., and compound data such as list, the dictionary can be the content of this file. The extension of this fie is ‘.yaml’. Multiple modules exist in Python to read the YAML file. The use of the PyYAML module to read the YAML file in Python has shown in this tutorial.

Pre-requisites:

Install the PyYAML module

PyYAML is the best module of Python to read the YAML file. PyYAML module is not installed with Python by default. So, you have to install this package before checking the examples of this tutorial. Run the following command to install PyYAML.

$ pip install pyyaml

Create a YAML file

Create a YAML file named client.yaml with the following content to use this file in the next part of this tutorial.

client.yaml

- name: Kamal Hossain

  email: kamal@gmail.com

  mobile: 01843456790

 

- name: Sakil Ahamed

  email: sakil@gmail.com

  mobile: 015662343423

 

- name: Mizanur Rahman

  email: mizan@gmail.com

  mobile: 01936784534

Example-1: Read the YAML content after Converting a python object

After installing the PyYAML package, the YAML module can be imported into the python script to read YAML content by converting a python object.  The dump() function of the yaml module is used to create the YAML content by serializing the content of the python object. Create a python file with the following script to generate and print the YAML stream by converting the content of the python object. The dump() function sorts the content of the dictionary based on the keys by default.

# Import YAML module

import yaml


# Declare a python object with data

books = [{'name': 'Think Python: An Introduction to Software Design', 'author': 'Allen B. Downey', 'price': '23'},

         {'name': 'Fluent Python: Clear, Concise, and Effective Programming', 'author': 'Luciano Ramalho', 'price': '50'},

         {'name': 'Think Python: An Introduction to Software Design', 'author': 'Allen B. Downey', 'price': '33'}

        ]


# Convert and print the JSON data in YAML stream

print(yaml.dump(books))

Output:

The following output will appear after executing the above script. The items of each dictionary of the python list have converted into each member of the YAML content. The content of the output has sorted based on the keys of the dictionary. For this, the value of the author key has been printed first, and the value of the price key has been printed last.

Example-2: Read the YAML content from a YAML file

The client.yaml file created in the previous part of this tutorial has been used in this example. Create a python file with the following script to read the sorted content of the client.yaml file based on the keys. The load() function has used in the script to read the full content of the client.yaml file. This function will return the content of the file as a python list of dictionaries. Next, the dump() function is used to convert the list into a YAML stream that has been printed later.

# Import YAML module

import yaml


# Load YAML data from the file

with open('client.yaml') as fh:

    read_data = yaml.load(fh, Loader=yaml.FullLoader)

# Print YAML data before sorting

print(read_data)


# Sort YAML data based on keys

sorted_data = yaml.dump(read_data)

# Print YAML data after sorting

print(sorted_data)

Output:

The following output will appear after executing the above script. After converting the content of the client.yaml file into a python list of dictionaries, each dictionary of the python list has converted into each member of the YAML content like the previous example. The value of the sort_key parameter of the dump() function is set to True by default. So, the output shows the sorted YAML content based on the keys.

Example-3: Read the keys and values from a YAML file

Create a python file with the following script to read and print the key and value separately from the client.yaml file. After loading the file’s content into the read_data variable, the item() function has used to read each key and the corresponding value from the content. The nested ‘for‘ loop has used to iterate the full content of the file and print the key-value pairs.

# Import YAML module

import yaml


# Load the YAML file

with open('client.yaml') as fh:

    # Load YAML data from the file

    read_data = yaml.load(fh, Loader=yaml.FullLoader)

    # Iterate the loop to read and print YAML data

    for i in range(0, len(read_data)):

        for key, value in read_data[i].items():

            print(key, ":", value)

        print('')

Output:

The following output will appear after executing the above script. The file’s content has not been sorted because the dump() function has not been used in the script.

Example-4: Read the YAML content into a list of dictionaries

The safe_load() function is used to convert the content of the YAML file into the python list of the dictionaries. This function can be used to load data from untrusted sources also.  Create a python file with the following script to load the content of a YAML file using the safe_load() function and print the loaded content.

# Import YAML module

import yaml


# Load the YAML file

with open('client.yaml') as fh:

    # Convert the YAML data into a dictionary

    dictionary_data = yaml.safe_load(fh)


# Print the dictionary data

print(dictionary_data)

Output:

The following output will appear after executing the above script. A list of dictionaries has been printed in the output.

Conclusion:

The ways to read YAML content from a python object and a file have been shown in this tutorial by using various examples. The concept of parsing the YAML file using the PyYAML package will be cleared for the python users after practicing the examples of this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.