Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

YAML File not loading with nested values

I am writing a YAML parser in python using ruamel.yaml
Reason for ruamel.yaml is it maintains comments which are key!

I know the issue is caused with the YAML file, and I am not sure on how to overcome this issue, or what a potential modification is. I have tried a few variations of indentation, but have struck out.

Keeping it simple, passing in source and trying to load it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Code:

import os
import os.path
import ruamel.yaml
from ruamel.yaml import YAML

def main():
    source = "my_yaml.yml"
    # Check if the source fil is valid yaml
    try:
        yaml = ruamel.yaml.YAML()
        with open(source) as fp:
            # data is the file data and will be used down the line
            data = yaml.load(fp)

        if data is None:
            data = {}
        return data
    except:
        print("Yaml file is not valid. Please check the source.")
        return 1

if __name__ == "__main__":
    out = main()

YAML that works:

d1: "key1"
d2: "key2"
d3: 
  d3_nest1: "key3"
  d3_nest2: "key4"

YAML that does not work:

d1: "key1"
d2: "key2"
d3: 
  d3_nest1: "key3"
  d3_nest2: "key4"
  # this next item breaks my yaml
  - d3_nest3: "key6"
  d3_nest4: "key7"

Other Variations of YAML:

d1: "key1"
d2: "key2"
d3: 
    d3_nest1: "key4"
    d3_nest2: "key5"
# this next item breaks my yaml
- d3_nest3: "key6"
    d3_nest4: "key7"

d1: "key1"
d2: "key2"
d3: 
    d3_nest1: "key4"
    d3_nest2: "key5"
# this next item breaks my yaml
    - d3_nest3: "key6"
        d3_nest4: "key7"

>Solution :

This:
d3_nest2: key5
– d3_nest3: key6
d3_nest4: key7

is invalid YAML because once you start a block style mapping, every line
needs to be of the form key: value until you change the indentation.

It is unclear what you want to achieve with providing a sequence element in the middle of a mapping, just as you cannot have a Python dict behave as a sequence for the 2nd element only. Maybe you want the following:

d1: "key1"
d2: "key2"
d3: 
  d3_nest1: "key3"
  d3_nest2:         # "key4" removed
  - d3_nest3: "key6"
  d3_nest4: "key7"

I recommend you create the data structure you want to have in Python (using nested dicts,lists and strings values), assuming you are more familiar with that, and then dump using ruamel.yaml so you’ll get a better idea of what is correct YAML syntax.

The recommended extension for files containing YAML documents has been .yaml for over 15 years.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading