Reading YAML with Python

Reading YAML with Python

Reading YAML with Python

What is YAML?

YAML (Yet Another Markup Language) is a highly readable data serialization format commonly used in configuration files. It expresses data structures in an easy-to-read format, supporting common data types such as lists and dictionaries. Compared to other data serialization formats such as JSON and XML, YAML is easier for humans to read and edit in text files.

YAML has the following features:

  • Succinctness: YAML uses indentation and line breaks to organize data, making it clearer and more compact, and reducing verbose markup.
  • Readability: YAML uses plain text to represent data, making it easier for humans to understand and edit.
  • Extensibility: YAML supports custom data types, allowing you to define new data structures as needed.

YAML Library in Python

In Python, we can use the PyYAML library to read and process data in the YAML format. PyYAML can load YAML files into Python objects and convert Python objects into YAML format.

To use the PyYAML library, you can install it via pip:

pip install pyyaml

Reading YAML Files

Using the PyYAML library, we can read the contents of a YAML file as a Python object. Below is a sample YAML file config.yml:

# config.yml

database:
host: localhost
port: 3306
username: my_username
password: my_password

app:
name: My App
version: 1.0

Now let’s read this YAML file and convert it into a Python object:

import yaml

with open('config.yml', 'r') as file:
config = yaml.load(file, Loader=yaml.FullLoader)

print(config)

Running result:

{'database': {'host': 'localhost', 'port': 3306, 'username': 'my_username', 'password': 'my_password'}, 'app': {'name': 'My App', 'version': 1.0}}

With this code, we’ve successfully loaded the YAML file into a Python dictionary object. Each key-value pair in the file is converted to a corresponding dictionary item.

Accessing YAML Data

Once the YAML file is loaded into a Python object, we can access the data just like a normal dictionary. Here are some common access methods:

# Access database configuration
database = config[‘database’]
print(database[‘host’]) # Output: localhost
print(database[‘port’]) # Output: 3306

# Access application configuration
app = config[‘app’]
print(app[‘name’]) # Output: My App
print(app[‘version’]) # Output: 1.0

Modifying YAML Data

We can modify the loaded YAML data and then save the modified data back to the YAML file. The following example demonstrates how to modify a configuration file:

# Change the database username and password
config['database']['username'] = 'new_username'
config['database']['password'] = 'new_password'

# Save the modified data
with open('config.yml', 'w') as file:
yaml.dump(config, file)

After running this code, the username and password in the config.yml file have been changed to the new values.

Summary

This article introduced how to use the PyYAML library in Python to read and process YAML-formatted data. With PyYAML, we can easily load YAML files as Python objects, access and modify the data within them, and save the modified data back to YAML files.

References

  • PyYAML Official Documentation: https://pyyaml.org/wiki/PyYAMLDocumentation

Leave a Reply

Your email address will not be published. Required fields are marked *