July 23, 2017 · YAML yml


YAML stands for YAML Ain't Markup Language, it is a data serialisation language and is deemed ideal (by some) for storing object trees but it has probably won more popularity due to it making data structures more readable to humans than both JSON and XML.

While more readable it is slower than any native serialization or JSON but for the typical use cases this will be irrelevant.

Basic Syntax
Interesting enough, there is an alternative syntax called "flow style/compress yaml" which also creates the situation that a valid JSON syntax will be valid YAML syntax. Which makes sense since YAML is a superset of JSON.

The root object (till the end of the file) will be a map which is the equivalent to dictionaries, hashes or objects.

For further syntax reference have a look at: Learn X in Y minutes - Where X=yaml
If the syntax fails you, or you fail the syntax, please have a look at: YAML Validator

  - list entry 1
  - list entry 2
#Compress the arrays with square brackets
list: [list entry 1, list entry 2]

    - dictionary_entry_1: value1
      dictionary_entry_2: value2
#Compress dictionaries with curly brackets
list: {dictionary_entry_1: value1, dictionary_entry_2: value2}

Reading YAML in python

filename = "filename.yml"
import yaml
from pprint import pprint

def read_yaml(filename):
    with open(filename) as f:
        return yaml.load(f)