0

I have a yaml that I need to process with a python script, the yaml is something like this:

user: john
description: Blablabla
version: 1
data: 
 data1: {type : bool, default: 0, flag: True}
 data2: {type : bool, default: 0, flag: True}
 data3: {type : float, default: 0, flag: false}

I need a list the the names of all the data that for example are bools or float, or all the ones where "flag" equals True or False, but I'm having difficulties moving around the list and getting what I need.

I've tried something like this:

   x = raw_data.items()
    for i in range(len(x['data'])):
        if (x['data'][i]).get('type') == "bool":
            print(x['data'][i])

But then I get an error: TypeError: 'dict_items' object is not subscriptable

Humusk
  • 5
  • 4
  • You need to use yaml.load(file). Please see an example [here](https://stackoverflow.com/questions/1773805/how-can-i-parse-a-yaml-file-in-python) – Adem kriouane Dec 01 '22 at 15:25

1 Answers1

0

Given a file called yaml.yaml containing your supplied yaml data:

user: john
description: Blablabla
version: 1
data: 
 data1: {type : bool, default: 0, flag: True}
 data2: {type : bool, default: 0, flag: True}
 data3: {type : float, default: 0, flag: false}

You can parse the file with PyYAML in a standard way, which is covered in other answers such as this one. For example:

import yaml

with open("yaml.yaml", "r") as stream:
    try:
        my_dict = yaml.safe_load(stream)
        print(my_dict)
    except yaml.YAMLError as exc:
        print(exc)

Yields a print out of your YAML as a dict:

$ python.exe yaml_so.py 
{'user': 'john', 'description': 'Blablabla', 'version': 1, 'data': {'data1': {'type': 'bool', 'default': 0, 'flag': True}, 'data2': {'type': 'bool', 'default': 0, 'flag': True}, 'data3': {'type': 'float', 'default': 0, 'flag': False}}}

Creating a list of all data items where flag == True can be accomplished by using dict.items() to iterate a view of the dictionary, for example:

for key, sub_dict in my_dict["data"].items():
    if "flag" in sub_dict:
        for sub_key, value in sub_dict.items():
            if sub_key == "flag" and value:
                print(f"sub_dict_entry: {key} -> key: {sub_key} -> value: {value}")

Yields:

sub_dict_entry: data1 -> key: flag -> value: True
sub_dict_entry: data2 -> key: flag -> value: True
  • The recommended extension for files containing YAML documents has been `.yaml` since at least September 2006 (see the yaml.org FAQ). YML is an XML derivative older than YAML. The PyYAML library you get with `import yaml` only supports a subset of YAML 1.1, which has been outdated since 2009. – Anthon Dec 02 '22 at 13:33
  • 1
    Thank you. You learn something new every day. I've revised my answer. – sandcountyfrank Dec 04 '22 at 14:37