Working With YAML File Format in Python

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

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

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

Here simple.json

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

Installing PyYAML with pip

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

YAML Comments

yaml support comments, example

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

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

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

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

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

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

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

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

generated YAML file (sports.yaml)

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

Excited With Python Linux, and Love Code, Writing and Many others

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store