Install the PyYAML installer
YAML in the python language has the PyYAML installer, download it here:/pypi/PyYAML
For networked windows, you can go directly to the terminal with win+r cmd. pip install PyYAML
Configure the yaml file
Go to the directory of the corresponding python file and create the yaml file (omitted)
Created through programming software
Customize the file extension to .yaml
Write the contents of the yaml file
city: shanghai time: 202210 successfully: 200 exception: 500
python reads yaml file
# Use utf-8 encoding # -*- coding:utf-8 -*- # Import yaml modules import yaml f = open('', 'r', encoding='utf-8') # Read the yaml file, encoded in utf-8 cfg = () # Read all the files d = (cfg, Loader=) # Use the load method to convert the contents of the read yaml file into a dictionary type # yaml5.1 abandoned the use of (file) because it was unsafe, and after 5.1 changed the need to specify a loader to disable the execution of arbitrary functions through the default loader (FullLoader) # Loader= Add this line of code and the alerts go away. print(d)
The result is a dictionary type
{'city': 'shanghai', 'time': 202210, 'successfully': 200, 'exception': 500}
Comparison of yaml file and python code writing
Basic yaml syntax rules:
key-value pair
case sensitive
Using indentation to indicate hierarchical relationships
The Tab key is not allowed for indentation, only spaces are allowed.
The number of spaces in the indentation does not matter, as long as elements at the same level are left-aligned
# means comments, from this character to the end of the line, will be ignored by the parser, this is the same as python comments
There are three data structures supported by yaml:
Object: a collection of key-value pairs, also known as mapping / hashes / dictionary
Array: a set of values arranged in a sequence, also known as a sequence / list.
Pure quantities (scalars): single, non-redivisible values. String, Boolean, Integer, Float, Null, Time, Date
Dictionary Nested Dictionaries
# Dictionary nested dictionaries #python code written as {'dd': 'city2:shanghai2 time2:2022102'} #dd: # city2: shanghai2 # time2: 2022102
Nested dictionaries in lists (or arrays, as they are called), a set of data in a dictionary
# Nested dictionaries in lists (or arrays) #pythonThe code is written as[{'city2': 'shanghai2'}, {'time2': 2022102}, {'successfully': 200}] #yaml write a list with a '-' sign in front of it - city2: shanghai2 - time2: 2022102 - successfully: 200
Nested dictionaries in lists (or arrays), multiple sets of data in dictionaries
# Nested dictionaries in lists (or arrays) The #python code is written as [{'city3': 'shanghai3', 'time3': 2022103}, {'city4': 'shanghai4', 'time4': 2022104}, {'city5': 'shanghai5', 'time5': 2022102}] - city3: shanghai3 time3: 2022103 - city4: shanghai4 time4: 2022104 - city5: shanghai5 time5: 2022102
Dictionary Nested Lists
# Dictionary nested lists The #python code is written as {'color1': ['red', 1], 'color2': ['bule', 2], 'color3': ['black', 3]} color1: - red - 1 color2: - bule - 2 color3: - black - 3
combine
# Combined use #python code written as {'languages': ['Ruby', 'Perl', 'Python', 'java c++ shell'], 'websites': {'YAML': '', 'Ruby': '', 'Python': '', 'Perl': ''}, 'db': {' host': 'xxx', 'port': 3306, 'user': 'shanghai', 'password': 'xxx', 'db_name': 'china', 'db_type': 'mysql'}} languages: - Ruby - Perl - Python - java c++ shell websites: YAML: Ruby: Python: Perl: db: host: xxx port: 3306 user: shanghai password: xxx db_name: china db_type: mysql
Other types
#1, numerical values are expressed directly in the form of literal quantities #number: 202210.010101 #{'number': 202210.010101} #2. Boolean values are expressed in terms of true and false #isSet: true #{'isSet': True} #isSet1: false #{'isSet1': False} #3. null is represented by ~ #parent: ~ #{'parent': None} #4. Time is in ISO8601 format. #time1: 2022-10-10t10:10:10.10-10:00 #{'time1': (2022, 10, 10, 10, 10, 10, 100000, tzinfo=((days=-1, seconds=50400)))} ##5. The date is expressed in year, month, and day in the composite iso8601 format. #date: 2022-10-01 #{'date': (2022, 10, 1)} #6, YAML allows the use of two exclamation points to force the conversion of a data type to a string str. #int_to_str: !!str 123 #{'bool_to_str': '123'} #bool_to_str: !!str true #{'bool_to_str': 'true'}
to this article on the use of python yaml format file method is introduced to this article, more related to the use of python yaml content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!