YAML file brief
- YAML is a very high readability, and programming language data structures are very close. Along with rich expression and scalable, and easy to use data markup language.
- YAML full name is actually "YAML Is not a Markup Language" (YAML is not a markup language) is a recursive acronym, it is emphasized that the data itself, rather than focusing mark
Why use YAML file?
In fact, YAML file is also a configuration file, but compared to the ini, conf configuration file, the more simple, easy to operate, but also to store different types of data; and like ini values are stored on the string type, read even after taking manual conversion
The basic syntax rules of YAML
- Case Sensitive
- Use indention hierarchy
- Not allowed to use the Tab key to indent, only spaces are allowed . (Your ide the tab key output may be replaced by four spaces)
- The number of spaces to indent does not matter, as long as the left elements are aligned to the same level
- # Denotes a comment
YAML support data structures
- Object: a set of keys, also known as mapping (mapping) / hashes (hashes) / Dictionary (dictionary)
- Array: a set of values are arranged in order, also known sequence (sequence) / list (list)
- Scalar (scalars): individual, can not be divided value
YAML, object data type
A set of key-value object of using the structure represented by colons.
animal: dogs
Python is converted into a data structure, as follows:
{'animal': 'dogs'}
All key-value pairs assignment.
hash: { name: Steve, foo: bar }
Python is converted into a data structure, as follows:
{'hash': {'name': 'Steve', 'foo': 'bar'}}
The list assignment
lists : [1,2,3]
Python is converted into a data structure, as follows:
{'lists': [1, 2, 3]}
Tuple assignment
tuples : (1,2,3)
Python is converted into a data structure, as follows:
{'tuples': '(1,2,3)'}
to sum up
- When the list of assignments, key-value pair data structure is converted into the Python directly when the list, using the dictionary;,
- When the assignment element group, the string is converted
- The final output is a dictionary, you can get the corresponding key values
YAML, array
Here that the structure of the array with [1,2,3,4] are not the same, as follows:
cool_list: - 10 - 15 - 12 hard_list: - {key: value} - [1,2,3] - test: - 1 - 2 - 3 twice_list: - - {a: b} - {c: d} - {e: f}
Python is converted into a data structure, as follows:
'cool_list': [10, 15, 12], 'hard_list': [ {'key': 'value'}, [1, 2, 3], { 'test': [1, 2, 3] } ], 'twice_list': [ [ {'a': 'b'}, {'c': 'd'}, {'e': 'f'} ] ]
to sum up
When you wrote the following such a data structure, will automatically turn into a dictionary, such as: { ' Key ' : ' value, val2 ' }
key:
value,val2
Yamla, 纯 量
Scalar is the most basic, the value can not be divided; substantially similar data types
- String
- Boolean value
- Integer
- Float
- time
- date
- Null
int: 12 float: 12.3 string: pets bool: true None: null time: 2001-12-14t21:59:43.10-05:00 date: 2018-03-21
Results of the
{ 'int': 12, 'float': 12.3, 'string': 'pets', 'bool': True, 'None': None, 'time': datetime.datetime(2001, 12, 14, 21, 59, 43, 100000, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=68400))), 'date': datetime.date(2018, 3, 21) }
It includes knowledge
- ISO 8601 format used between the time, date, and time using a T-connector, on behalf of the last used time zone +
- Date must be in ISO 8601 format, i.e. yyyy-MM-dd
- bool type: true, True, false, False can
- Can ~ represent null
YAML, string Comments
- String is the most complicated, but it is the most common data type.
- Default string without using quotation marks '' '' wrapped
The basis of the wording
str: This is his string
Python is converted into a data structure, as follows:
{ ' STR ' : ' What is his string ' }
Use quotation marks under what circumstances? When the string contains spaces or special characters , etc.
str_s: " string contains special characters, spaces & @ # $% ^ & * ()! "
Python is converted into a data structure, as follows:
{ ' Str_s ' : ' string that contains spaces & special characters @ # $% ^ & * ()! ' }
Note that double quotes "" does not escape special characters
str1: 'test\n1' str2: "test\n2"
Python is converted into a data structure, as follows:
{'str1': 'test\\n1', 'str2': 'test\n2'}
Can split multiple lines, each line is a space, but be aware that their level must be consistent
st_string: hello
world
Python is converted into a data structure, as follows:
{ 'st_string': 'hello world' }
YAML, type a strong turn
YAML allows two exclamation points, cast data types
is: !!str 123 sf: !!float '12.22' si: !!int '222'
Execution results are as follows
{ 'is': '123', 'sf': 12.22, 'si': 222 }