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.
# Why YAML?
From my article on The Rise of the Declarative Data Stack:
YAML, Yet Another Markup Language, has become the configuration markup language for most modern tools. The reasons are simple: Compared to its predecessors, XML and JSON, which are still highly used, YAML is less verbose.
Besides their different reasons for use, XML is designed to support structured documents, while JSON should be simple and universal and can quickly be processed; JSON has been mainly used for small data sets and REST services. YAML has similar threats but tries to be a superset of JSON, although every JSON can effectively be a valid YAML file.
But why is YAML used for declarative configurations? It supports lists and dictionaries with almost no overhead. YAML is optimized for reading extended configurations. Its descriptive and portable structure across different programming languages and its clear interface make it easy to maintain, read, and modify, making it well-suited for this task.
YAML is also used when implementing a DSL (Domain Specific Language) in your application. A DSL abstracts the complexity behind a system. It is a general-purpose language aimed at any software problem. Like Markdown or another example of HTML, it’s programming language agnostic; the engine, the browser, does not care how you generated the HTML; it knows what to do with it. That’s the goal of DSL, too.
# 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.
