Open In App

Reading and Writing YAML File in Python

Last Updated : 13 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

YAML (YAML Ain't Markup Language) is a human-readable data serialization standard that is commonly used for configuration files and data exchange between languages with different data structures. YAML is preferred in many cases due to its simplicity and readability compared to other formats like JSON or XML. In Python, working with YAML files is straightforward thanks to the PyYAML library. This article guides us through reading from and writing to YAML files using Python.

Installing PyYAML

Before we start, make sure we have the PyYAML library installed. We can install it using pip:

pip install pyyaml

Writing Data to a YAML File in Python

Let's start by writing some data to a YAML file. We will create a Python dictionary and then save it as a YAML file.

Python
import yaml

# Data to be written to the YAML file
data = {
    'name': 'Amit',
    'age': 25,
    'city': 'Mumbai',
    'skills': ['Python', 'Data Analysis', 'Machine Learning']
}

# Writing the data to a YAML file
with open('data.yaml', 'w') as file:
    yaml.dump(data, file)

print("Data has been written to 'data.yaml'")

Output:

Data has been written to 'data.yaml'
Screenshot-2024-09-13-150944
writing to yaml file in Python

Reading Data from a YAML File in Python

Now, let's read the data from the YAML file we just created.

Python
import yaml

# Reading data from the YAML file
with open('data.yaml', 'r') as file:
    loaded_data = yaml.safe_load(file)

print("Data read from 'data.yaml':")
print(loaded_data)

Output:

Screenshot-2024-09-13-151136
Reading from YAML file in Python


Reading YAML with Nested Structures

YAML files can also have nested structures like dictionaries within dictionaries. Let’s look at an example of reading a more complex YAML file.

Example YAML (nested_data.yaml):

person:
name: Raj
details:
age: 30
city: Delhi
skills:
- HTML
- CSS
- JavaScript

Python Code to Read Nested YAML:

Python
import yaml

# Reading nested data from a YAML file
with open('nested_data.yaml', 'r') as file:
    nested_data = yaml.safe_load(file)

print("Nested Data read from 'nested_data.yaml':")
print(nested_data)

Output:

Nested Data read from 'nested_data.yaml':
{'person': {'name': 'Raj', 'details': {'age': 30, 'city': 'Delhi', 'skills': ['HTML', 'CSS', 'JavaScript']}}}

Writing Nested Data to a YAML File

We can also write nested data structures back to a YAML file:

Python
import yaml

# Nested data
nested_data = {
    'person': {
        'name': 'Sita',
        'details': {
            'age': 28,
            'city': 'Kolkata',
            'skills': ['Data Science', 'SQL', 'TensorFlow']
        }
    }
}

# Writing nested data to a YAML file
with open('nested_data.yaml', 'w') as file:
    yaml.dump(nested_data, file)

print("Nested data has been written to 'nested_data.yaml'")

Output:

Nested data has been written to 'nested_data.yaml'
Screenshot-2024-09-13-151438
Writing nested data to YAML in Python

Conclusion

YAML is a flexible and human-readable data format, making it ideal for configuration files and data exchange. With the PyYAML library, Python developers can easily read from and write to YAML files, making it simple to integrate YAML into their projects. Whether we're handling simple configuration settings or complex data structures, PyYAML provides the tools we need to work effectively with YAML in Python.

By understanding how to read and write YAML files in Python, we can take advantage of YAML's simplicity and power in our own projects.


Next Article
Article Tags :
Practice Tags :

Similar Reads