Open In App

Python yaml Module

Last Updated : 08 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

YAML (YAML Ain’t Markup Language) is a human-readable data serialization format that is commonly used for configuration files, data exchange, and more. Python provides a convenient module called yaml for parsing and serializing YAML data. In this article, we will understand the yaml module, discuss its features, and provide illustrative examples of its usage.

What is Python yaml Module?

The yaml module in Python provides functions for parsing YAML data into Python objects (yaml.load()) and serializing Python objects into YAML format (yaml.dump()). It supports various YAML features, including mappings, sequences, and scalars.

We can install the Python YAML module by using the following command:

pip install PyYAML

YAML Module in Python

Below are some of the examples by which we can understand about YAML module in Python:

Parsing YAML from a File

geeksforgeeks.yml

UserName: GeeksforGeeks
Password: GFG@123
Phone: 1234567890
Website: geeksforgeeks.org
Skills:
-Python
-SQL
-Django
-Javascript

In this example, we use yaml.load() to parse YAML data from a file. The Loader=yaml.FullLoader argument is used to load the YAML data securely.

Python3
import yaml

# YAML file path
yaml_file = 'geeksforgeeks.yml'

# Reading YAML data from file
with open(yaml_file, 'r') as f:
    yaml_data = yaml.load(f, Loader=yaml.FullLoader)

# Displaying parsed YAML data
print(yaml_data)

Output:

{'UserName': 'GeeksforGeeks', 'Password': 'GFG@123', 'Phone': 1234567890, 
'Website': 'geeksforgeeks.org', 'Skills': '-Python -SQL -Django -Javascript'}

Serializing Python Objects to YAML

Here, we use yaml.dump() to serialize a Python dictionary into YAML format. The resulting YAML data can be written to a file or used elsewhere as needed.

Python3
import yaml

# Python dictionary
data = {'name': 'John', 'age': 30, 'city': 'New York'}

# Serializing dictionary to YAML format
yaml_data = yaml.dump(data)

# Displaying YAML data
print(yaml_data)

Output:

age: 30 
city: New York
name: John

Handling Multi-Document YAML Files

In this example, we use yaml.load_all() to parse a multi-document YAML string. Each document in the string is parsed individually, and we iterate through the parsed data to display each document.

Python3
import yaml

# Multi-document YAML string
yaml_string = """
name: John
age: 30
---
name: Alice
age: 25
"""

# Parsing multi-document YAML string
yaml_data = yaml.load_all(yaml_string, Loader=yaml.FullLoader)

# Displaying parsed YAML data
for doc in yaml_data:
    print(doc)

Output:

{'name': 'John', 'age': 30} 
{'name': 'Alice', 'age': 25}

Serializing Python Objects with Custom Formatting

Python yaml.dump() serialize a Python dictionary into YAML format with custom formatting. Setting default_flow_style=False ensures that the YAML output uses a block style instead of inline style for sequences and mappings.

Python3
import yaml

# Python dictionary
data = {'name': 'John', 'age': 30, 'city': 'New York'}

# Serializing dictionary to YAML format with custom formatting
yaml_data = yaml.dump(data, default_flow_style=False)

# Displaying YAML data
print(yaml_data)

Output:

age: 30 
city: New York
name: John

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads