Search

Search IconIcon to open search

YAML

Last updated by Simon Späti

YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

It stands for Yet Another Markup Language.

# The language of declarative configuration

See Descriptive Configs - YAML.

# Reading with Python

# Reading and parsing a YAML file with Python

Once we have the YAML parser imported, we can load a YAML file and parse it. YAML files usually carry the extension .yaml or .yml. Let’s work with the following example YAML file called config.yaml:

1
2
3
4
5
rest:
  url: "https://example.org/primenumbers/v1"
  port: 8443

prime_numbers: [2, 3, 5, 7, 11, 13, 17, 19]

Loading, parsing, and using this configuration file is similar to loading JSON with the  Python JSON library. First, we  open the file. Next, we parse it with the yaml.safe_load() function. Please note that I changed the output a little to make it more readable for you:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> import yaml
>>> with open('config.yml', 'r') as file
...    prime_service = yaml.safe_load(file)

>>> prime_service
{'rest': 
  { 'url': 'https://example.org/primenumbers/v1',
    'port': 8443
  },
  'prime_numbers': [2, 3, 5, 7, 11, 13, 17, 19]}

>>> prime_service['rest']['url']
https://example.org/primenumbers/v1

The YAML parser returns a regular Python object that best fits the data. In this case, it’s a  Python dictionary. This means all the regular dictionary features can be used, like using get() with a default value.

# YAML Multiline

https://yaml-multiline.info/

Comment:

  • I like plain text, which has the advantage to include " or ' for SQL’s. And the limitiations are not that bad with : and # :
    • Plain flow scalars are picky about the : and # characters. They can be in the string, but : cannot appear before a space or newline, and # cannot appear after a space or newline; doing this will cause a syntax error. If you need to use these characters you are probably better off using one of the quoted styles instead.

# Flow Scalars

# Single-quoted

YAML

1
2
3
4
5
example: 'Several lines of text,\n
··containing ''single quotes''. Escapes (like \n) don''t do anything.\n
··\n
··Newlines can be added by leaving a blank line.\n
····Leading whitespace on lines is ignored.'\n

Result

Several lines of text, containing ‘single quotes’. Escapes (like \n) don’t do anything.\n
Newlines can be added by leaving a blank line. Leading whitespace on lines is ignored.

# Double-quoted

YAML

1
2
3
4
5
6
7
example: "Several lines of text,\n
··containing \"double quotes\". Escapes (like \\n) work.\nIn addition,\n
··newlines can be esc\\n
··aped to prevent them from being converted to a space.\n
··\n
··Newlines can also be added by leaving a blank line.\n
····Leading whitespace on lines is ignored."\n

Result
Several lines of text, containing “double quotes”. Escapes (like \n) work.\n
In addition, newlines can be escaped to prevent them from being converted to a space.\n
Newlines can also be added by leaving a blank line. Leading whitespace on lines is ignored.\n

# Plain

YAML

1
2
3
4
5
6
example: Several lines of text,\n
··with some "quotes" of various 'types'.\n
··Escapes (like \n) don't do anything.\n
··\n
··Newlines can be added by leaving a blank line.\n
····Additional leading whitespace is ignored.\n

Result

Several lines of text, with some “quotes” of various ’types’. Escapes (like \n) don’t do anything.\n
Newlines can be added by leaving a blank line. Additional leading whitespace is ignored.

Note: Plain flow scalars are picky about the : and # characters. They can be in the string, but : cannot appear before a space or newline, and # cannot appear after a space or newline; doing this will cause a syntax error. If you need to use these characters you are probably better off using one of the quoted styles instead.


References: