DEV Community

Cover image for YAML Tutorial | Using YAML With Python | PyYAML
Tharun Shiv
Tharun Shiv

Posted on • Edited on

YAML Tutorial | Using YAML With Python | PyYAML

In this Article (Click to skip to that topic):

What is YAML?

What if you had a very easy, simple, readable way to feed data dynamically to your program, regardless of the programming language using which it is written?? All of this by just writing a file with no programming knowledge?

YAML: YAML Ain't Markup Language. YAML is a human-friendly way to send data such as configuration data to your program. Just by editing the YAML file, the program will be able to read the values from the YAML File and use it.

Ever used JSON files? YAML can be considered a more readable version of JSON files.

Why do you need a YAML File?

Consider this situation. You have a program which installs a set of software onto your computer. This program can be used on many computers. Let us assume that, these are the computer-specific data that is necessary to run the program on the computer:

  1. This Computer's username
  2. This Computer's password
  3. The path in which the software must be installed in
  4. The specific set of software that has to be installed in this particular computer

If you were using the usual programming way of doing this, you would either

  • Take in the inputs through some command-line arguments ( which is difficult for non-programmers to use )
  • Change the code every time you run on different machines ( that isn't a good way at all )

So what if I told you, you can have a text file, which is human readable, independent of the programming language that you are using, win which you can just fill in the above 4 data according to the computer, and the program will take care of the rest?
That is exactly what YAML helps you with.

Creating your first YAML File

Step 1: Create a file by any name, say config.yaml. Make sure to use the extension .yaml to the file.

Step 2: A YAML file should start with the --- like

    ---
    # YAML files start with ---
    # comments begin with a #
    # and they can be written anywhere
Enter fullscreen mode Exit fullscreen mode

Step 3: You can have the keys in the YAML file, which may or may not hold any values in them. Below we have keys such as username, password, path, VLC Media Player etc., which we can use later in the program to grab their respective values.

    ---
    username: Shiv
    password: shiv@456
Enter fullscreen mode Exit fullscreen mode

Note: YAML is case sensitive.

Step 4: You can have a list of items under a key, even nest them further

    ---
    username: Shiv
    password: shiv@456
    path:
        - VLC Media Player: /etc/vlc
        - Visual Studio Code: /etc/vscode
        - Google Chrome: /etc/chrome
    softwares:
        - VLC Media Player
        - Visual Studio Code
        - Google Chrome
        - Git Bash
        - Video Convertor
        - Node Js
Enter fullscreen mode Exit fullscreen mode

So using the YAML syntax, we have easily listed the data we will need, to feed to our application.

Step 5: Now just save this file as config.yaml

How to use PyYAML to use YAML Files?

Now we have the config.yaml, all that we have to do is use it in our program. Say you are writing a Python program that uses this config file and installs the software. We will have to use a python package called PyYaml.

Step 1: Install pip ( Install Python from their website first if you don't have it )

  • Download get-pip.py to a folder on your computer.
  • Open a command prompt and navigate to the folder containing get-pip.py.
  • Run the following command:

python3 get-pip.py

  • Pip is now installed!

Step 2: Install PyYaml package now

pip3 install pyyaml

Step 3: Create a new Python File and open it - say test.py and add the following code to it, where we import using PyYAML

    import yaml     # import pyyaml package

    # open the yaml file and load it into data

    with open('config.yaml') as f:
        data = yaml.load(f, Loader=yaml.FullLoader)
        print(data)
Enter fullscreen mode Exit fullscreen mode

Output:

    {
        'username': 'Shiv',
        'path': [
            {'VLC Media Player': '/etc/vlc'},
            {'Visual Studio Code': '/etc/vscode'},
            {'Google Chrome': '/etc/chrome'}
        ],
        'password': 'shiv@456',
        'softwares': [
            'VLC Media Player',
            'Visual Studio Code',
            'Google Chrome',
            'Git Bash',
            'Video Convertor',
            'Node Js'
        ]
    }
Enter fullscreen mode Exit fullscreen mode

Step 4: Let us try to pick what we need from the config file

    from __future__ import print_function
    import yaml

    with open('config.yaml') as f:
        data = yaml.load(f, Loader=yaml.FullLoader)

        username = data['username']
        password = data['password']

        # printing the username
        print("The username is ", username)
        print("The password is ", password)
Enter fullscreen mode Exit fullscreen mode

Output:

    The username is Shiv
    The password is shiv@456
Enter fullscreen mode Exit fullscreen mode

Here we have grabbed the username and password from the data. Next, let us grab the software list from the config file.

Step 5: Grabbing the software list

    from __future__ import print_function
    import yaml

    with open('config.yaml') as f:
        data = yaml.load(f, Loader=yaml.FullLoader)

        username = data['username']
        password = data['password']
        path_list = data['path']
        software_list = data['softwares']

        # printing the username
        print("The username is ", username)
        print("The password is ", password)

        # the software list
        for software in software_list:
            print("Installing ", software, "...")
Enter fullscreen mode Exit fullscreen mode

Output:

    The username is  Shiv
    The password is  shiv@456
    Installing  VLC Media Player ...
    Installing  Visual Studio Code ...
    Installing  Google Chrome ...
    Installing  Git Bash ...
    Installing  Video Convertor ...
    Installing  Node Js ...
Enter fullscreen mode Exit fullscreen mode

Since the softwares are stored in a list, we can easily run a for loop and grab each software in the software variable and print it.

Step 6: Printing the paths where each software must be installed

Now, this isn't complex, so just analyze it. We are first looping over the list of items in the path_list, and then for each of those list item, say {'VLC Media Player': '/etc/vlc'}, we will now loop over softwares in the software_list to find if the software matches the key of this list item, so if 'VLC Media Player' from the path_list matches the 'VLC Media Player' from the software_list, then it prints the path of it. ( Read and analyze it again if you didn't get it, you will get it for sure. )

    from __future__ import print_function
    import yaml

    with open('config.yaml') as f:
        data = yaml.load(f, Loader=yaml.FullLoader)

        username = data['username']
        password = data['password']
        path_list = data['path']
        software_list = data['softwares']

        # printing the username
        print("The username is ", username)
        print("The password is ", password)

        # the paths are
        for path_dictionary in path_list:
            for software in software_list:
                if software in path_dictionary:
                    print("Install ",software," in ",path_dictionary[software])


        # the software list
        for software in software_list:
            print("Installing ", software, "...")
Enter fullscreen mode Exit fullscreen mode

Output: ( drum rolls please....)

    The username is  Shiv
    The password is  shiv@456
    Install  VLC Media Player  in  /etc/vlc
    Install  Visual Studio Code  in  /etc/vscode
    Install  Google Chrome  in  /etc/chrome
    Installing  VLC Media Player ...
    Installing  Visual Studio Code ...
    Installing  Google Chrome ...
    Installing  Git Bash ...
    Installing  Video Convertor ...
    Installing  Node Js ...
Enter fullscreen mode Exit fullscreen mode

Advantages of using YAML and PyYAML? Why Should you use them?

1. Readable and easy to write

The YAML file is easy to read as it doesn't contain any extra symbols other than the essential colons and spaces

2. Write once, use everywhere

The same config file can be used for any programming language without modifications.

3. Change values easily

Just by changing the values in one YAML file, it will get reflected in the program, thus need not go through the pain of changing values everywhere in the program, or search for the variable in the program that holds this value.

Have a JSON file and wanna convert to YAML?

Check out this website where you can convert JSON into YAML : JSON to YAML - online convertor

Article originally written by Tharun Shiv, on https://tharunshiv.com

Written by,

Thank you for reading, This is Tharun Shiv a.k.a Developer Tharun

Tharun Shiv

Top comments (13)

Collapse
 
chandrika56 profile image
Jaya chandrika reddy

Which do you think is more used in the Industry? YAML vs JSON

Collapse
 
developertharun profile image
Tharun Shiv

As far as I've noticed, JSON is used in places where the data transfer takes place , and the data is parsed, and the user doesn't manually deal with it.
YAML files are used when user deals with providing inputs or configuration.

See the main advantage of YAML is readability. So the user is less prone to errors like missing a comma or missing a curly bracket. πŸ™‚

Collapse
 
nicolasini profile image
Nico S___

JSON is used for anything API related. Any payload of data transferred between servers, clients, or any other API is very likely to be JSON, unless its XML
YAML is generally used for configuration purposes, although JSON is quite popular for that purpose too.

Collapse
 
developertharun profile image
Tharun Shiv

Yes, that's on point. Thanks πŸ™‚

Collapse
 
reactifystudio profile image
Reactify

JSON. But for DevOps you will need yaml

Collapse
 
baso53 profile image
Sebastijan Grabar • Edited

I absolutely hate YAML. I thought it was only going to be like that only when I started using it, that it would get better with time, but it really hasn't.

My personal opinion:

  • isn't more readable than JSON
  • editor support is an abomination (VS Code and IntelliJ), it never gets the indentation right
  • extremely beginner unfriendly, I bet no one who is looking at YAML for the first won't be sure what is the syntax for what
  • a looot of stuff is valid YAML and there are many ways to write the same things (arrays, I'm looking at you), which again, leads to confusion
  • the YAML spec is HUGE and it contains the whole JSON spec inside of it, which again, just leads to confusion
Collapse
 
nicolasini profile image
Nico S___

I agree with you in the points you dislike. They are definitely an issue.
However, it is more readable than JSON for non-developers. And sometimes that is more important.

Collapse
 
developertharun profile image
Tharun Shiv

Yes Nico. You just give them a template and they will easily fill it in, in case of YAML.

Collapse
 
developertharun profile image
Tharun Shiv

Thanks for sharing your perspective Sebastijan. πŸ™‚

Collapse
 
reactifystudio profile image
Reactify • Edited

Me too.
I find it's indentation poor. Curly braces are works for me. I can easily follow the nesting πŸ’―

Collapse
 
bnorikane profile image
Bruce N Norikane

Excellent explanation of how YAML is used! Your example makes everything very clear.
Thanks

Collapse
 
developertharun profile image
Tharun Shiv

Thanks a lot Bruce! This motivates me to write more... πŸ™‚

Collapse
 
reactifystudio profile image
Reactify • Edited

I don't think it's readable than JSON.
I find it hard to follow. Due to lack of curly braces. I dislike indentation poor when nesting.