Search
YAML
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
:
|
|
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:
|
|
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
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.
- Plain flow scalars are picky about the
# Flow Scalars
# Single-quoted
YAML
|
|
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
|
|
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
|
|
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: