Working With YAML File Format in Python

Feri Lukmansyah
5 min readJan 25, 2021
image from unsplash

using python for data formatting it's a good idea in this blog we will see how to work with YAML file, and how to process YAML file format in python, now we’re going to break it down into several stages, including

  • what is YAML
  • Installing PyYAML
  • Understanding YAML File
  • Read YAML File in Python
  • Read YAML Documents
  • YAML dump in Python
  • Writing YAML File
  • YAML Sorting Keys

After knowing some steps above, I assume that I have installed the python interpreter on each device, then let’s start

What is YAML

YAML (YAML Ain’t Markup Language) is human-readable data serialization language it's commonly used for configuration file, for example look a docker compose file, here docker-compose.yml

version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"

this file used to store a configuration of docker container, this is useful when we deploy an application using docker not reconfigure it repeatedly.

when you ask why yaml file not json file, the answer is, YAML is easier to read and understand than json

YAML file vs JSON file

Here is example a YAML file and JSON, check this out, what is the difference of YAML file and JSON file

file simple.yml

Origin:
author: Dan Brown
language: English
publication-date: 2017-10-03
pages: 461
description: | When billionaire researcher Edmond Kirsch is killed,
it is up to Robert Landon & Ambra Vidal to honor
his memory by making public his findings concerning the
origin of human life and its destiny.

Here simple.json

{
"Origin": {
"author": "Dan Brown",
"language": "English",
"publication-date": "2017-10-03",
"pages": 461,
"description": "When billionaire researcher Edmond Kirsch is killed,
it is up to Robert Langdon and Ambra Vidal to honor
his memory by making public his findings concerning
the origin of human life and its destiny."
}
}

Installing PyYAML

this library used to interact python with YAML file, where we can do YAML manipulation like read, write, dump and many others, this library can founds in here https://pypi.org/project/PyYAML/

installing with pip

pip install PyYAML
Installing PyYAML with pip

Understanding YAML File

in this part we will learn about the parts of the YAML file

Whitespace and indentation

whitespace is a part of YAML formatter, Unless otherwise indicated, newlines indicate the end of a field.

Example if you structure a YAML document with indentation.

The indentation level can be one or more spaces. The specification forbits tabs treat them differently

Let's create YAML file called foo.yml

foo: bar
please: help
stuff:
foo: bar
bar: foo

YAML Comments

yaml support comments, example

# this is comments in yaml file
# file foo.yaml
foo: bar
please: help
stuff:
foo: bar
bar: foo

YAML File Manipulation

in this step we will learn how to manipulate YAML file using python (pyyaml library) include

  • how to read YAML file
  • writing YAML file
  • Sorting YAML file

let’s start in first step

Read YAML File

in this section we will learn how to read YAMLfile using python, for example we have two YAML formatted file like this

# filename: sports.yaml
- sports:
- soccer
- football
- basketball
- cricket
- hockey
- table tennis
- countries:
- Pakistan
- USA
- India
- China
- Germany
- France
- Spain

new after have a YAML file we can read the file as usual, it’s just a little modified like this

# load & read yaml
with open('sports.yaml', 'r') as yaml_file:
sport_list = yaml.load(yaml_file, Loader=yaml.FullLoader)

# get result yaml file
print(sport_list)

now run python script, in my case main.py so, run with theese command

python main.py

now we can see the result like this

result for read YAML file

In the script above we specified yaml.FullLoader as the value for the Loader parameter which loads the full YAML language

in second example we will use full_load function inside pyyaml library, well in this second example we can read two different yaml data formats in one file by performing nested loops, for example like this

# full load yaml file
with open('sports.yaml', 'r') as file:
yaml_load = yaml.full_load(file)

# iterate list
for datas in yaml_load:

# getting key, and value
for key, value in datas.items():
print(key, value)

new run python script to see result

read and iterate YAML file using full_load function

Sorting YAML file

now in this section we will learn how to sorting YAML file using python (pyYAML). So far we have two examples that produce two data formats, namely dict and list, now we will learn to sort so that the data can be read according to the yaml format and produce a new file with the appropriate format, we will sorting the keys using sort_keys params, we can see the application in the example below

# sorting yaml
with open('sports.yaml', 'r') as files:
data_yaml = yaml.load(files, Loader=yaml.FullLoader)

# sorting yaml
sort_file = yaml.dump(data_yaml, sort_keys=True)

# get result
print(sort_file)

run python script to see result

result sorted keys in yaml files

Writing YAML file

after learn how to read YAML file now in this section we will learn how to writing YAML file in Python, now create a list of dictionary, where list has two ditionaries with different keys and has a value in the form of a list, for example like this

# data dict
dict_file = [{'sports': ['soccer', 'football', 'basketball', 'cricket', 'hockey', 'table tennis']},
{'countries': ['Pakistan', 'USA', 'India', 'China', 'Germany', 'France', 'Spain']}]

we can use the dump function as in json, but use the yamlmodule theese the example

# data dict
dict_file = [{'sports': ['soccer', 'football', 'basketball', 'cricket', 'hockey', 'table tennis']},
{'countries': ['Pakistan', 'USA', 'India', 'China', 'Germany', 'France', 'Spain']}]

# write yaml
with open('sports.yaml', 'w+') as file:
docx = yaml.dump(dict_file, file)

now run python script then sports.yaml will be generated automatically

generated YAML file (sports.yaml)

Conclusion

work with YAML files using python, reads better than JSON and we can write, read, sort and much more that can be done in python, just like JSON files, hopefully useful and good luck, written by Feri Lukmansyah

--

--