Python Intermediate and Advanced

Python Intermediate_004_ Guide to PyYAML

codeaddict 2024. 12. 30. 22:16

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

  1. Reading: The yaml.safe_load() function parses the user.yaml file into a Python dictionary.
  2. 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

  1. Reading: Nested YAML structures are directly converted into Python dictionaries containing other dictionaries. Access nested elements using keys.
  2. Modifying: You can update nested keys like config['server']['debug'] to change specific values.
  3. 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

  1. Reading: The yaml.safe_load() function reads lists within YAML files as Python lists, allowing iteration.
  2. Processing: Each employee is processed individually, demonstrating how to access nested data within lists.
  3. 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!