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
Similar Reads
Python Modules Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c
7 min read
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Reloading modules in Python The reload() is a previously imported module. If you've altered the module source file using an outside editor and want to test the updated version without leaving the Python interpreter, this is helpful. The module object is the return value. Reloading modules in Python2.xreload(module)For above 2.
1 min read
Import module in Python In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
__future__ Module in Python __future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. This module includes all the latest functions which were not present in the previous version in Python. And we can use this by importing the __future__ module. I
4 min read
Parse a YAML file in Python YAML is the abbreviation of Yet Another Markup Language or YAML ain't markup Language which is the data format used to exchange data. YAML can store only data and no commands. It is similar to the XML and JSON data formats. In this article, we will dive deep into the concept of parsing YAML files in
4 min read
Basics Of Python Modules A library refers to a collection of modules that together cater to a specific type of needs or application. Module is a file(.py file) containing variables, class definitions statements, and functions related to a particular task. Python modules that come preloaded with Python are called standard li
3 min read
Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
9 min read
External Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
5 min read
How to Install a Python Module? A module is simply a file containing Python code. Functions, groups, and variables can all be described in a module. Runnable code can also be used in a module. What is a Python Module?A module can be imported by multiple programs for their application, hence a single code can be used by multiple pr
4 min read