Register Login

Reading and Writing YAML to a File in Python

Updated Oct 19, 2023

YAML, short for Yet Another Markup Language, is a data serialization language that comes under one of the most popular programming languages. In recent years, this language has become a popular source of storing data in a systematic format.

YAML is a data format, and users can load the data stored in a YAML file by loading that data into a Python object. This article will clearly explain how to read and write YAML to a File in Python.

Prerequisites for reading and writing a YAML file in Python

Install the pyyaml

The first approach to installing the pyyaml library is as follows:

Use the pip package manager to install the library. Run the command if users have pip in their system:

$ pip install pyyaml

The second approach to installing the pyyaml library is as follows:

If users do not have pip installed in their system, they can use the library's source page to download the repository as a zip file. Then go to the terminal or command prompt, and navigate to the directory where users have downloaded the file. Lastly, run the following command:

$ Python install

Install VS Code or Visual Studio Code to run the YAML file

Step 1: Go to the official website of the VS Code and download the specific version on the OS.

Step 2: It will start the download process. Once the download is complete, open the file and accept the agreement.

Step 3: Specify the drive locations for installing the VS Code.

Step 4: Proceed further by selecting a folder name for the VS Code setup files.

Step 5: In the "Select Additional Tasks," select options according to your choice.

Step 6: Click on the Install option to start the process.

Step 7: Click Finish.

Creating a YAML file

Handling and manipulating YAML files using Python is similar to working with other data formats. Users can read the data stored in the YAML file. But before moving to this process, users first follow the steps given below to start creating a YAML file:

Step 1: Create a YAML file using an online editor like jsonformattor or codebeautify.


Step 2: Use the given YAML template, copy-paste it into the online YAML validator, and check the validity.

apiVersion: apps/v1
kind: Deployment
  name: nginx-deployment
    app: nginx
  replicas: 3
      app: nginx
        app: nginx
      - name: nginx
        image: nginx:1.14.2
        - containerPort: 80

Step 3: Once you check the validity, download the file to create a valid YAML file and work with Python.

Reading the YAML File Using Python

We have created a YAML file with the following contents:

- student_names:
- Abhisekh
- Pritam
- Pallavi
- Manish
- Gautam
- Siddharth
- address:
- Delhi
- Assam
- Bangalore
- Mumbai
- Chennai
- Kolkata


Now, we will import the YAML file data to the PyCharm suing the following code:

import yaml
with open(r'D:\Stekies\data.yaml') as file:
student_names = yaml.load(file, Loader=yaml.FullLoader)



In the code example mentioned above, we used the yaml.FullLoader for the Loader parameter as the value. This loader loads the overall YAML language. But it will avoid arbitrary code execution.

The loader will pass the yaml.FullLoader as the value for the Loader parameter, instead of using the load function and then passing. Users can also apply the full_load() function instead of the load() function.

The function will return all the elements in the YAML file into a Python dictionary.

The second YAML file contains the following contents:

student_names: Marks
Abhisekh: 76
Pritam: 82
Manish: 93
Pallavi: 97
Siddharth: 58
Using the following code, we will read the YAML file data similarly:
import yaml
with open(r'D:\Stekies\data.yaml') as data:
demo = yaml.full_load(data)
for elements, doc in demo.items():
print(elements, ":", doc)



In this example, we used the full_load() for parsing the contents of the given file with the path. Then, we used the for loop to access each file element. The function will return all the elements in the YAML file into a Python dictionary.

Writing the YAML File Using Python

We hope the above section has given a deep-dyed vision of creating and validating a YAML file and reading the contents in Python. Let us try another technique to store data in a YAML file.

Here, we will arrange the data the other way around, i.e., serialize a Python dictionary and save them into a YAML formatted file. Look at the given code snippet and grab the explanation to understand in-depth:

import yaml
student_name = [{'Students name': ['A', 'B', 'C', 'D', 'E', 'F']},
{'Marks': [87, 76, 68, 95, 60, 79]}]
with open(r'D:\Stekies\yaml_sample.yaml', 'w') as data:
res = yaml.dump(student_name, data)




We used a variable "student_names" and inserted data in dictionary format. Then we used the open() function with the path of the YAML file where we will save the file.

Lastly, the dump() method accepts the Python dictionary as the first and a File object as the second parameter. Once we run the code, it will create a YAML file with all the data elements we have inserted into the Python dictionary in the given working directory.

Users can also use the sort_keys parameter, an excellent option for writing YAML files in Python.

Code Snippet:

import yaml
with open(r'D:\Stekies\data.yaml') as file:
demo = yaml.load(file, Loader=yaml.FullLoader)
res = yaml.dump(demo, sort_keys=True)



The above example has sorted the data elements in the alphabetical order.

Why do users use YAML with Python?

Users often wonder whether they can configure files systematically. But there are several options, but one of the best is YAML. It is perfect for configuration files. Many developers and programmers recommend using this file format for this purpose.

Large projects, like Docker and Kubernetes, use YAML to illustrate deployments. Due to its richer syntax, it is affable to the eyes and simple to read, write, and parse.

But despite all the above facts, the YAML with Python has the following disadvantages –

  • XML and JSON are a part of the Python standard library but not YAML.
  • Its structure is entirely dependent on indentation and is often frustrating.
  • The functionality of the YAML file is too simple and versatile, like data exchange of simple elements.


We hope this Python article has given a crisp idea of how to read and write YAML files using Python. Before starting the procedure, users should install Python's YAML library (pyyaml) to use YAML formatted files. The article highlighted all the steps associated with installing the library.

Then, users should install an interface where they can run and see the contents of the YAML file (here, we used VS Code). The steps are easy for downloading and installing the program. Using the functions, we read and write the contents of a YAML file into our Python program as dictionaries.