YAML - BKJackson/BKJackson_Wiki GitHub Wiki

Multiple YAML Documents Read by Python

  • A document starts with three dashes and ends with three periods. The end operator is usually optional.
import yaml

if __name__ == '__main__':
    stream = open("foo.yaml", 'r')
    dictionary = yaml.load_all(stream)

    for doc in dictionary:
        print("New document:")
        for key, value in doc.items():
            print(key + " : " + str(value))
            if type(value) is list:
                print(str(len(value))) 
  • The compound YAML document:
---
bar: foo
foo: bar
...
---
one: two
three: four
  • Python print output:
New document:
bar : foo
foo : bar
New document:
one : two
three : four