Read values from yaml file python

There is not a standard way of doing this, and this is for a large part caused by the fact that the keys of YAML can be complex. This makes path matching methods that work for much simpler formats like JSON unusable.

If your YAML is "tag-less", like yours, it still allows much more complex structures than JSON, but you can implement walking recursively over the collection types of a YAML file [sequence and mapping] fairly easily, and while doing so explicitly match indices resp. keys and/or elements resp. values:

import ruamel.yaml as yaml

def _do_not_care[]:
    pass

def find_collection[d, key=_do_not_care, value=_do_not_care, results=None]:

    def check_key_value[d, k, v, results]:
        # print['checking', key, value, k, d[k], results]
        if k == key:
            if value in [_do_not_care, v]:
                results.append[d]
                return
        elif key == _do_not_care and v == value:
            results.append[d]
            return
        if isinstance[v, [dict, list]]:
            find_collection[v, key, value, results]

    if results is None:
        results = []
    if isinstance[d, dict]:
        for k in d:
            check_key_value[d, k, d[k], results]
    if isinstance[d, list]:
        for k, v in enumerate[d]:
            check_key_value[d, k, v, results]
    return results

def find_first[d, key=_do_not_care, value=_do_not_care]:
    ret_val = find_collection[d, key, value]
    return ret_val[0] if ret_val else {}

def find_value_for_key[d, key]:
    return find_first[d, key][key]

with the above in place you can do:

file_name = 'SampleCase.yml'
with open[file_name, 'r'] as f:  
    data = yaml.safe_load[f]
for d in find_collection[data, value='Tug']:
    vessel_type = find_first[data, key='Name', value=d['VesselType']]
    port_propeller = find_first[vessel_type, key='Name', value='Port Propeller']
    print['Tug -> MaxRPM', find_value_for_key[port_propeller, key='MaxRPM']]

this prints [assuming the input is corrected, see point 1. ]:

Tug -> MaxRPM 1800

There are a few things to keep in mind:

  1. Your YAML is invalid, as there is no --- separation between the directive and the document. It first three lines should look like:

    %YAML 1.1
    ---
    VesselTypes:
    

    However it is probably not necessary to specify the directive at all: PyYAML still doesn't support YAML 1.2 after seven years and your YAML doesn't seem to have anything YAML 1.1 specific.

  2. You are using PyYAML's load[] without Loader argument, which can be unsafe if you have no control over the input. You should always use safe_load if you can [like with your source].

The above was tested using ruamel.yaml [a superset of PyYAML supporting YAML 1.2 as well as 1.1. Disclaimer: I am the author of that package]. I should work as is with PyYAML if you have to stick with that.

The full form of YAML is Yet Another Mark-up Language. This file format is very popular now to store serialized data that is human-readable. It is mainly used for configuration files, but it can be used for other purposes also. Different types of scalar data such as number, string, etc., and compound data such as list, the dictionary can be the content of this file. The extension of this fie is ‘.yaml’. Multiple modules exist in Python to read the YAML file. The use of the PyYAML module to read the YAML file in Python has shown in this tutorial.

Pre-requisites:

Install the PyYAML module

PyYAML is the best module of Python to read the YAML file. PyYAML module is not installed with Python by default. So, you have to install this package before checking the examples of this tutorial. Run the following command to install PyYAML.

Create a YAML file

Create a YAML file named client.yaml with the following content to use this file in the next part of this tutorial.

client.yaml

- name: Kamal Hossain

  email: kamal@gmail.com

  mobile: 01843456790

  - name: Sakil Ahamed

  email: sakil@gmail.com

  mobile: 015662343423

  - name: Mizanur Rahman

  email: mizan@gmail.com

  mobile: 01936784534

Example-1: Read the YAML content after Converting a python object

After installing the PyYAML package, the YAML module can be imported into the python script to read YAML content by converting a python object.  The dump[] function of the yaml module is used to create the YAML content by serializing the content of the python object. Create a python file with the following script to generate and print the YAML stream by converting the content of the python object. The dump[] function sorts the content of the dictionary based on the keys by default.

# Import YAML module

import yaml

# Declare a python object with data

books = [{'name': 'Think Python: An Introduction to Software Design', 'author': 'Allen B. Downey', 'price': '23'},

         {'name': 'Fluent Python: Clear, Concise, and Effective Programming', 'author': 'Luciano Ramalho', 'price': '50'},

         {'name': 'Think Python: An Introduction to Software Design', 'author': 'Allen B. Downey', 'price': '33'}

        ]

# Convert and print the JSON data in YAML stream

print[yaml.dump[books]]

Output:

The following output will appear after executing the above script. The items of each dictionary of the python list have converted into each member of the YAML content. The content of the output has sorted based on the keys of the dictionary. For this, the value of the author key has been printed first, and the value of the price key has been printed last.

Example-2: Read the YAML content from a YAML file

The client.yaml file created in the previous part of this tutorial has been used in this example. Create a python file with the following script to read the sorted content of the client.yaml file based on the keys. The load[] function has used in the script to read the full content of the client.yaml file. This function will return the content of the file as a python list of dictionaries. Next, the dump[] function is used to convert the list into a YAML stream that has been printed later.

# Import YAML module

import yaml

# Load YAML data from the file

with open['client.yaml'] as fh:

    read_data = yaml.load[fh, Loader=yaml.FullLoader]

# Print YAML data before sorting

print[read_data]

# Sort YAML data based on keys

sorted_data = yaml.dump[read_data]

# Print YAML data after sorting

print[sorted_data]

Output:

The following output will appear after executing the above script. After converting the content of the client.yaml file into a python list of dictionaries, each dictionary of the python list has converted into each member of the YAML content like the previous example. The value of the sort_key parameter of the dump[] function is set to True by default. So, the output shows the sorted YAML content based on the keys.

Example-3: Read the keys and values from a YAML file

Create a python file with the following script to read and print the key and value separately from the client.yaml file. After loading the file’s content into the read_data variable, the item[] function has used to read each key and the corresponding value from the content. The nested ‘for‘ loop has used to iterate the full content of the file and print the key-value pairs.

# Import YAML module

import yaml

# Load the YAML file

with open['client.yaml'] as fh:

    # Load YAML data from the file

    read_data = yaml.load[fh, Loader=yaml.FullLoader]

    # Iterate the loop to read and print YAML data

    for i in range[0, len[read_data]]:

        for key, value in read_data[i].items[]:

            print[key, ":", value]

        print['']

Output:

The following output will appear after executing the above script. The file’s content has not been sorted because the dump[] function has not been used in the script.

Example-4: Read the YAML content into a list of dictionaries

The safe_load[] function is used to convert the content of the YAML file into the python list of the dictionaries. This function can be used to load data from untrusted sources also.  Create a python file with the following script to load the content of a YAML file using the safe_load[] function and print the loaded content.

# Import YAML module

import yaml

# Load the YAML file

with open['client.yaml'] as fh:

    # Convert the YAML data into a dictionary

    dictionary_data = yaml.safe_load[fh]

# Print the dictionary data

print[dictionary_data]

Output:

The following output will appear after executing the above script. A list of dictionaries has been printed in the output.

Conclusion:

The ways to read YAML content from a python object and a file have been shown in this tutorial by using various examples. The concept of parsing the YAML file using the PyYAML package will be cleared for the python users after practicing the examples of this tutorial.

About the author

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

How do you read a value from a YAML file in Python?

We can read the YAML file using the PyYAML module's yaml. load[] function. This function parse and converts a YAML object to a Python dictionary [ dict object]. This process is known as Deserializing YAML into a Python.

How do you parse and extract data from a YAML file in Python?

How to parse and extract data from a YAML file in Python.
a_yaml_file = open["example.yaml"].
parsed_yaml_file = yaml. load[a_yaml_file, Loader=yaml. FullLoader].
print[parsed_yaml_file["a_dictionary"]].
print[parsed_yaml_file. get["a_list"]].

How do I read a YAML file?

YAML is a digestible data serialization language often used to create configuration files with any programming language. Designed for human interaction, YAML is a strict superset of JSON, another data serialization language. But because it's a strict superset, it can do everything that JSON can and more.

How do I convert a YAML file to a dictionary in Python?

Just use python-benedict , it's a dict subclass that provides I/O support for most common formats, including yaml . Show activity on this post. bios is reading the raw data from a file and converting a python dict object.

Chủ Đề