Handle null or empty values in YAML files using Python

Managing YAML files often involves handling null or empty values to maintain data integrity.

This tutorial guides you through parsing, writing, and ignoring such values using Python.

 

 

Parse YAML files with null or empty values

To manage explicit null values in a YAML file, use the PyYAML library.

Here’s how you can load a YAML file containing various null representations:

import yaml
yaml_content = """
users:
  - name: Ahmed
    email: null
  - name: Fatima
    email: ~
  - name: Karim
    email: ""
"""
data = yaml.safe_load(yaml_content)
print(data)

Output:

{'users': [{'name': 'Ahmed', 'email': None}, {'name': 'Fatima', 'email': None}, {'name': 'Karim', 'email': ''}]}

PyYAML converts null and ~ to Python None, while empty strings remain as empty strings.

 

Write YAML files with null or empty values

Represent null values in YAML output

When writing YAML files, represent None values as null using PyYAML:

import yaml
data = {
    'users': [
        {'name': 'Sara', 'email': None},
        {'name': 'Youssef', 'email': 'youssef@example.com'}
    ]
}
yaml_output = yaml.dump(data)
print(yaml_output)

Output:

users:
- email: null
  name: Sara
- email: youssef@example.com
  name: Youssef

None values are serialized as null in the YAML output.

 

Ignore null or empty values

To exclude null or empty values while parsing, filter them out after loading:

import yaml
yaml_content = """
users:
  - name: Hani
    email: null
  - name: Salma
    email: salma@example.com
"""
data = yaml.safe_load(yaml_content)
filtered_users = [{k: v for k, v in user.items() if v} for user in data['users']]
print(filtered_users)

Output:

[{'name': 'Salma', 'email': 'salma@example.com'}]

Only includes the pair in the new dictionary if the value v is truthy (i.e., not None, not an empty string, not 0, not False, etc.).

Exclude null values when writing YAML files

To omit null or empty values during writing, preprocess the data to remove such entries:

import yaml
data = {
  'users': [
      {'name': 'Amira', 'email': None},
      {'name': 'Mohamed', 'email': 'mohamed@example.com'},
      {'name': 'Laila', 'email': ''}
  ]
}
cleaned_data = {
  'users': [{k: v for k, v in user.items() if v is not None} for user in data['users']]
}
yaml_output = yaml.dump(cleaned_data, default_flow_style=False)
print(yaml_output)

Output:

users:
- name: Amira
- email: mohamed@example.com
  name: Mohamed
- email: ''
  name: Laila

Entries with None is removed.

Leave a Reply

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