YAML (YAML Ain't Markup Language) is a human-readable data serialization format. Python provides excellent support for working with YAML through various libraries. This guide will introduce you to using YAML in Python, focusing on the popular PyYAML
library.
To get started with YAML in Python, you'll need to install the PyYAML library. Use pip to install it:
pip install pyyaml
PyYAML makes it easy to parse YAML data into Python objects. Here's a simple example:
import yaml
yaml_string = """
name: John Doe
age: 30
skills:
- Python
- YAML
- Docker
"""
data = yaml.safe_load(yaml_string)
print(data)
This code snippet demonstrates how to parse a YAML string into a Python dictionary. The safe_load()
function is used for safe loading of YAML content, which is recommended for untrusted input.
PyYAML also allows you to convert Python objects into YAML format:
import yaml
data = {
'name': 'Jane Smith',
'age': 28,
'is_employee': True,
'departments': ['HR', 'Finance']
}
yaml_output = yaml.dump(data, default_flow_style=False)
print(yaml_output)
The dump()
function converts Python objects to YAML. Setting default_flow_style=False
ensures a more readable, block-style output.
YAML is commonly used for configuration files. Here's how to read from and write to YAML files:
import yaml
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
print(config)
import yaml
data = {'server': 'localhost', 'port': 8080}
with open('config.yaml', 'w') as file:
yaml.dump(data, file)
safe_load()
instead of load()
when parsing untrusted YAML to prevent YAML injection attacks.YAML in Python offers a powerful way to work with structured data. Whether you're dealing with configuration files, data serialization, or complex data structures, PyYAML provides the tools you need. As you become more comfortable with YAML in Python, explore advanced features like YAML tags and multiple documents to fully leverage its capabilities.