Python YAML – Write, Read, Parse YAML

A human-readable data serialization format called YAML stands for YAML Ain’t Markup Language. The format is popular for configuring and storing data, especially in software development. YAML files can be read, written, and parsed using Python’s yaml module.

The purpose of this blog is to show how to read, write, and parse YAML using Python. Let’s get started with the following content:

  • How to Read YAML?
  • How to Write YAML?
  • How to Parse YAML?

Before we can use PyYAML, we need to install it. You can install PyYAML using pip, which is the standard package manager for Python.

pip install pyyaml

How to Read YAML?

The “load()” method of the “PyYAML” module is used to read a YAML file’s contents into a Python object. Following is a snippet of the YAML file that will be read:

The below example code is used to read YAML file data:

Code:

import yaml
with open('sample.yaml') as f:
    data = yaml.full_load(f)
    for item, val in data.items():
        print(item, ":", val)
  • The module named “yaml” is imported into the program.
  • The “open()” function opens the YAML file named “sample.yaml” and is stored in the object “f”.
  • The “yaml.full_load()” loads a YAML file in the Python dictionary object by taking the file object as an argument.
  • The for loop is used with the “dict.items()” to return the element value of the Python object.

Output:

The YAML file contents have been read successfully.

How to Write YAML?

To write YAML data to a file, the “yaml.dump()” method of the “PyYAML” module is used in Python. Here is an example code:

Code:

import yaml
data = {"name": "Joseph", "age": 23, "height": 5.7}
with open("example.yaml", "w") as f:
    yaml.dump(data, f)
  • The YAML data is initialized as a dictionary and stored in a variable named “data”.
  • The “open()” function creates a new YAML file in “w” writing mode and writes the data to a file using the “yaml.dump()” method.

Output:

Data has been added to the YAML file successfully.

How to Parse YAML?

The “yaml.load()” method can be used to parse YAML data in Python. Here is an example code:

Code:

import yaml
yaml_data = """
name: Joseph
age: 30
height: 6.1
city: berlin
"""
parsed_yaml_data = yaml.load(yaml_data, Loader=yaml.FullLoader)
print(parsed_yaml_data)
  • The “yaml” module is imported, and the yaml data string is initialized and stored in a variable named “yaml_Data”.
  • The “yaml.load()” is used to parse the YAML data and stored in a variable named “parsed_yaml_data”.

Output:

The YAML data has been parsed successfully.

Conclusion

The “yaml.load()”, “yaml.dump()”, and “yaml.full_load()” of the “PyYAML” module is used to read, write and parse YAML files in Python. The “yaml.load()” function reads and parses YAML files by converting YAML objects to Python dictionary objects. The “yaml.dump()” function is used along with the “open()” function to write the given data into a YAML file. This post presented an in-depth guide on reading, writing, and parsing YAML data in Python.