Introduction
YAML (Yet Another Markup Language) is a human-readable data standard for serializing data that provides an easy way to store and transfer structured data between systems. In this article, we will explore the basics of working with YAML files in Python, including reading, writing, and manipulating YAML data using popular libraries such as PyYAML and Ruamel.YAML.
YAML is a simple, human-readable data format that can be used for storing configuration files, data interchange between systems, and serializing Python objects. It is widely supported by various programming languages and has become an essential tool in the data management process.
Table of Contents
- Introduction
- Installing YAML Libraries in Python
- YAML Example File
- Reading YAML Files in Python with PyYAML
- Reading YAML Files in Python with Ruamel.YAML
- Writing YAML Files in Python
- Written YAML File
- Manipulating YAML Data in Python
- Conclusion
Installing YAML Libraries in Python
To work with YAML files in Python, we need to install one of the popular libraries: PyYAML or Ruamel.YAML. Both libraries provide similar functionality but have some differences in performance and features. Here’s how to install them using pip:
# Install PyYAML
pip install pyyaml
# Install Ruamel.YAML
pip install ruamel.yaml
YAML Example File
# config.yml
version: "3.2"
services:
web:
hostname: mywebapp
image: myorg/mywebapp:latest
Reading YAML Files in Python with PyYAML
To read a YAML file using the PyYAML library, we can use the safe_load()
function from the yaml
module. Here’s an example:
import yaml
with open('config.yml', 'r') as f:
data = yaml.safe_load(f)
print(data)
Reading YAML Files in Python with Ruamel.YAML
Ruamel.YAML provides a similar way to read and write YAML files. Here’s an example using the load()
function:
from ruamel.yaml import YAML
with open('config.yml', 'r') as f:
yaml = YAML(typ='safe')
data = yaml.load(f)
print(data)
Writing YAML Files in Python
To write a YAML file using PyYAML, we can use the dump()
function from the yaml
module:
import yaml
customer_dict = {
'name': 'Jennifer Doe',
'age': 35,
'address': {
'street': '12 Green Duck Drive',
'city': 'Auckland',
'postal_code': 1010
}
}
with open('customer.yml', 'w') as f:
yaml.dump(customer_dict, f)
Written YAML File
address:
city: Auckland
postal_code: 1010
street: 12 Green Duck Drive
age: 35
name: Jennifer Doe
Ruamel.YAML also provides a simple way to write YAML files using the Path
module and dump()
function:
from ruamel.yaml import YAML
from pathlib import Path
customer_dict = {
'name': 'Jennifer Doe',
'age': 35,
'address': {
'street': '12 Green Duck Drive',
'city': 'Auckland',
'postal_code': 1010
}
}
yaml = YAML()
output_path = Path("./customer.yml")
with output_path.open('w') as fp:
yaml.dump(customer_dict, fp)
Manipulating YAML Data in Python
Once we have loaded the data from a YAML file, we can manipulate it as any other Python data structure. Here’s an example of updating a value in a nested dictionary:
import yaml
with open('config.yml', 'r') as f:
data = yaml.safe_load(f)
data['services']['web']['image'] = 'mysql:latest'
with open('updated_config.yml', 'w') as f:
yaml.dump(data, f)
Conclusion
In this article, we have covered the basics of working with YAML files in Python using popular libraries like PyYAML and Ruamel.YAML. We discussed reading, writing, and manipulating YAML data in Python. By understanding these concepts, you can efficiently manage your structured data using YAML files in various applications.