Open In App

How To Append Data To Yaml File Using Python

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

We are given a YAML file and our task is to append data inside that YAML file by using different approaches in Python. In this article, we will see how we can append data to YAML file using Python.

We have data.yml file containing the following data and we will append the data inside this file.

data.yaml

GeeksforGeeks: Education

Append Data To Yaml File Using Python

Below are some of the ways by which we can append data to YAML file using Python:

  1. Using YAML Python Module
  2. Using File I/O Operations
  3. Using PyYAML Library
  4. Using ruamel.yaml Library

Using the YAML Python Module

This code defines a function append_to_yaml to safely append data to a YAML file. It first loads existing data from the file, appends new data to it, and then dumps the combined data back into the file, ensuring valid YAML syntax with default_flow_style=False. This approach maintains the integrity of the YAML file by handling existing content and avoiding potential syntax issues.

Python3




import yaml
 
 
def append_to_yaml(file_path, data_to_append):
    with open(file_path, 'a+') as file:
        file.seek(0)
        existing_data = yaml.load(file, Loader=yaml.FullLoader) or []
        existing_data.append(data_to_append)
        file.seek(0)
        yaml.dump(existing_data, file, default_flow_style=False)
 
 
file_path = 'data.yaml'
data_to_append = {'key': 'value'}
append_to_yaml(file_path, data_to_append)


data.yaml

GeeksforGeeks: Education
key: value

Using File I/O Operations

This script defines a function append_to_yaml that appends YAML-serialized data_to_append to a file specified by file_path. It directly writes the YAML text of the data to the file in append mode, potentially resulting in invalid YAML syntax if the file already contains data or multiple YAML documents.

Python3




import yaml
 
def append_to_yaml(file_path, data_to_append):
    with open(file_path, 'a') as file:
        yaml_text = yaml.dump(data_to_append)
        file.write(yaml_text)
 
file_path = 'data.yaml'
data_to_append = {'key': 'value'}
append_to_yaml(file_path, data_to_append)


data.yaml

GeeksforGeeks: Education
key: value

Using PyYAML Library

This code defines a function append_to_yaml that appends data to a YAML file. It opens the file specified by file_path in append mode, then dumps the data_to_append dictionary into the file using the yaml.dump() function. However, this approach may not work reliably for appending to a YAML file, as it could result in invalid YAML syntax if the file already contains YAML data.

Python3




import yaml
 
def append_to_yaml(file_path, data_to_append):
    with open(file_path, 'a') as file:
        yaml.dump(data_to_append, file)
 
file_path = 'data.yaml'
data_to_append = {'key': 'value'}
append_to_yaml(file_path, data_to_append)


data.yaml

GeeksforGeeks: Education
key: value

Using ruamel.yaml Library

This updated code utilizes the `ruamel.yaml` library for more reliable appending of data to a YAML file. It creates a `YAML` instance, which handles the dumping of data to the file specified by `file_path`. By using `ruamel.yaml`, it ensures that the appended data maintains valid YAML syntax even if the file already contains YAML content.

Python3




import ruamel.yaml
 
 
def append_to_yaml(file_path, data_to_append):
    with open(file_path, 'a') as file:
        yaml = ruamel.yaml.YAML()
        yaml.dump(data_to_append, file)
 
 
file_path = 'data.yaml'
data_to_append = {'key': 'value'}
append_to_yaml(file_path, data_to_append)


data.yaml

GeeksforGeeks: Education
key: value


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads