YAML basic with python examples
Peer uest
Cancel
Sign Up
Forget passwd??
Log In
blog
11
YAML basic with python examples
0

YAML Ain’t Markup Language (YAML) is a human readable serialization language. It’s often used as a format for configuration files. 

A Simple Example

---
company: Apple Inc.
number_of_employees: 100000
private: true

locations:
  - US
  - Canada
  - China

products:
  iPhone:
    iPhone_11: 999  
    iPhone_10: 799
    iPhone_8: 699
  iPad:
    iPad_pro: 999
    iPad: 599
    iPad_mini: 399
  music: streaming service

The file starts with three dashes. Each set of dashes as the beginning of a new YAML document.

The next line that makes a key-value pair. Company is a key that points to a string value: Apple Inc.

YAML supports different value types. The file starts with three key-value pairs. They have different data types.

Company is a string (you can enclose a string with double-quotes. But it is not necessary).

number_of_employees is a integer.

private is a boolean. 

location is an array with three elements, each denoted by an opening dash.

The elements in location are indedented with two spaces. Indentation is how YAML denotes nesting.

products has three elements. Two dictionaries (iPhone and iPad) and a string (music). YAML supports mixed types.

Let's give a deep dive on YAML. 

1. Indentation

A YAML document is structured with indentation. The indentation level can be one or more spaces. Tab cannot be used as indentation.

Consider the following example

 

 

0
Add a Comment
1 Comments

Test the comment

×
×
×
×
Sign In