Open In App

Compare Keys In Two Yaml Files And Print Differences?

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

YAML (YAML Ain’t Markup Language) files are widely used for configuring applications and storing data in a human-readable format. When dealing with multiple YAML files, it’s common to compare them to identify differences, especially when managing configurations across different environments or versions. In this article, we’ll explore generally see some examples in Python to compare keys in two YAML files and print their differences.

Compare Keys In Two Yaml Files And Print Differences?

Below, are the examples of how to compare keys in two Yaml Files and Print Differences

  • Using Python’s yaml Library
  • Using deepdiff Libraries
  • Using jsondiff Libraries

file1.yaml

key1: value1
key2: value2
key3: value3

file2.yaml

key1: value1
key4: value4
key5: value5

Compare Keys In Two Yaml Files Using Python’s Yaml Library

In this example, the function compare_yaml_keys takes two YAML files as input, loads their contents, and then compares the keys present in each file, printing out the keys unique to each file.

Python3
import yaml


def compare_yaml_keys(file1, file2):
    with open(file1, 'r') as f1, open(file2, 'r') as f2:
        yaml1 = yaml.safe_load(f1)
        yaml2 = yaml.safe_load(f2)

        keys1 = set(yaml1.keys())
        keys2 = set(yaml2.keys())

        keys_only_in_file1 = keys1 - keys2
        keys_only_in_file2 = keys2 - keys1

        print("Keys only in", file1, ":", keys_only_in_file1)
        print("Keys only in", file2, ":", keys_only_in_file2)


# Example Usage:
compare_yaml_keys("file1.yaml", "file2.yaml")

Output

Keys only in file1.yaml : {'key3', 'key2'}
Keys only in file2.yaml : {'key4', 'key5'}

Compare Keys In Two Yaml Files Using deepdiff Libraries

In this example, the function compare_yaml_with_deepdiff loads two YAML files, then utilizes the DeepDiff library to identify and print the differences between their contents, ignoring the order of elements.

Python3
import yaml
from deepdiff import DeepDiff


def compare_yaml_with_deepdiff(file1, file2):
    with open(file1, 'r') as f1, open(file2, 'r') as f2:
        yaml1 = yaml.safe_load(f1)
        yaml2 = yaml.safe_load(f2)

        diff = DeepDiff(yaml1, yaml2, ignore_order=True)

        print("Differences between", file1, "and", file2, ":")
        print(diff)


# Example Usage:
compare_yaml_with_deepdiff("file1.yaml", "file2.yaml")

Output

Differences between file1.yaml and file2.yaml :
{'key4': 'value4', 'key5': 'value5', delete: ['key2', 'key3']}

Compare Keys In Two Yaml Files Using jsondiff Libraries

In this example, the function compare_yaml_with_jsondiff loads two YAML files and utilizes the jsondiff library to identify and print the differences between their contents in JSON format.

Python3
import yaml
import jsondiff


def compare_yaml_with_jsondiff(file1, file2):
    with open(file1, 'r') as f1, open(file2, 'r') as f2:
        yaml1 = yaml.safe_load(f1)
        yaml2 = yaml.safe_load(f2)

        diff = jsondiff.diff(yaml1, yaml2)

        print("Differences between", file1, "and", file2, ":")
        print(diff)


# Example Usage:
compare_yaml_with_jsondiff("file1.yaml", "file2.yaml")

Output

{'dictionary_item_added': [root['key4'], root['key5']],
'dictionary_item_removed': [root['key2'], root['key3']]}


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads