Introduction
When dealing with configuration files or structured data, YAML (short for “YAML Ain’t Markup Language”) is a popular format due to its simplicity and readability. YAML organizes data in a human-readable manner with minimal syntax, using indentation and symbols like : for key-value pairs, - for lists, and nesting for hierarchy. In Python, the PyYAML library allows seamless interaction with YAML files for reading and writing structured data.
In this blog post, we will walk through three examples, starting from simple to advanced, showcasing how to read from and write to YAML files using PyYAML. Each example will include clear explanations of the YAML structure and the corresponding Python code.
Prerequisites
Before diving in, ensure you have installed PyYAML. Use the following command to install it via pip:
pip install pyyaml
1. Example1: Reading and Writing a Flat YAML File
Create a YAML File
Create a file named user.yaml in the same directory as your Python script and add the following content:
name: Alice
age: 30
is_developer: true
Code: Reading and Writing
import yaml
# Reading from a YAML file
with open('user.yaml', 'r') as file:
user_data = yaml.safe_load(file)
print("Loaded YAML Data:", user_data) # Output: {'name': 'Alice', 'age': 30, 'is_developer': True}
# Writing back to a new YAML file
updated_data = {
'name': 'Alice',
'age': 31, # Update the age
'is_developer': True
}
with open('updated_user.yaml', 'w') as file:
yaml.dump(updated_data, file)
print("Updated YAML saved to 'updated_user.yaml'")
Explanation
- Reading: The yaml.safe_load() function parses the user.yaml file into a Python dictionary.
- Writing: The yaml.dump() function serializes the updated dictionary back into a YAML file.
This approach is ideal for flat YAML files without nested structures.
2. Example2: Handling Nested YAML Data
Create a YAML File
Create a file named config.yaml and add the following content:
server:
host: localhost
port: 8000
debug: true
database:
name: app_db
user: admin
password: secret
Code: Reading and Writing
import yaml
# Reading nested YAML data
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
print("Database Config:", config['database']) # Output: {'name': 'app_db', 'user': 'admin', 'password': 'secret'}
# Modifying and writing nested YAML data
config['server']['debug'] = False # Disable debug mode
config['database']['password'] = 'new_secret' # Update password
with open('updated_config.yaml', 'w') as file:
yaml.dump(config, file)
print("Updated YAML saved to 'updated_config.yaml'")
Explanation
- Reading: Nested YAML structures are directly converted into Python dictionaries containing other dictionaries. Access nested elements using keys.
- Modifying: You can update nested keys like config['server']['debug'] to change specific values.
- Writing: The modified data is written back into a YAML file, preserving the nested structure.
This method is perfect for hierarchical configurations like server settings or application environments.
3. Example3: Working with Lists in YAML
YAML Context
Now, consider a YAML file named employees.yaml that includes a list of employees:
employees:
- name: John Doe
age: 28
skills:
- Python
- Django
- name: Jane Smith
age: 35
skills:
- JavaScript
- React
Code: Reading and Writing
import yaml
# Reading YAML with lists
with open('employees.yaml', 'r') as file:
employees_data = yaml.safe_load(file)
for employee in employees_data['employees']:
print(f"Employee: {employee['name']}, Skills: {', '.join(employee['skills'])}")
# Adding a new employee to the list
new_employee = {
'name': 'Alice Green',
'age': 30,
'skills': ['Flask', 'SQLAlchemy']
}
employees_data['employees'].append(new_employee)
with open('updated_employees.yaml', 'w') as file:
yaml.dump(employees_data, file)
print("Updated YAML saved to 'updated_employees.yaml'")
Explanation
- Reading: The yaml.safe_load() function reads lists within YAML files as Python lists, allowing iteration.
- Processing: Each employee is processed individually, demonstrating how to access nested data within lists.
- Writing: A new employee is appended to the employees list, and the updated data is saved back into a YAML file.
This approach is particularly useful for datasets with repetitive structures, such as employee records, product catalogs, or inventory lists.
Conclusion
PyYAML simplifies working with YAML files, whether you’re dealing with flat key-value pairs, nested configurations, or complex lists. By understanding how to read, modify, and write YAML files, you can leverage YAML’s human-readable format for configurations, data storage, or application settings.
Explore PyYAML in your projects to create cleaner, more maintainable configurations — and let YAML handle the structure for you!
'Python Intermediate and Advanced' 카테고리의 다른 글
Python Intermediate_004_ Nested Classes (0) | 2025.01.12 |
---|---|
Python Intermediate_005: Working with the Requests Library (3) | 2025.01.04 |
Python Intermediate_003: Filter Function (0) | 2024.12.23 |
Python Intermediate_002: Map Function (파이썬 고급 - Map 함수) (3) | 2024.12.22 |
Python Intermediate_002: Map Function (English Version) (0) | 2024.12.22 |