Python yaml module

Python yaml module

Introduction to yaml files

yaml is a specialized language for writing configuration files.

yaml file rules

  • Case-sensitive.
  • Use of indentation to indicate hierarchical relationships.
  • Use the space bar to indent, not the Tab key to indent
  • The number of indented spaces is not fixed, only the left-hand alignment of elements at the same level is required.
  • Strings in a file do not need to be marked with quotation marks, but if they contain special characters they need to be marked with quotation marks.
  • Comments marked with #

yaml file data structure

  • Object: a collection of key-value pairs (referred to as a "mapping or dictionary")

    Key-value pairs are represented by a colon ":" structure, with a space separating the colon from the value

  • Array: a set of values arranged in order (referred to as a "sequence or list")
    Arrays are preceded by the "-" symbol, with a space separating the symbol from the value

  • scalars: single, non-divisible values (e.g., strings, bool values, integers, floating point numbers, time, date, null, etc.)
    None values can be represented by null or ~

Reading yaml configuration files in python

Prerequisites

Before reading yaml files in python you need to install pyyaml and import the yaml module.

  • The module you need to install to use yaml is pyyaml (pip3 install pyyaml);
  • The imported module is yaml (import yaml)

Read yaml file data

python reads the file data by open and then converts the data into a list or dictionary by the load function.

import yaml
import os

def get_yaml_data(yaml_file):
    # Open yaml file
    print("***Get yaml file data***")
    file = open(yaml_file, 'r', encoding="utf-8")
    file_data = file.read()
    file.close()

    print(file_data)
    print("Type:", type(file_data))

    # Converting strings to dictionaries or lists
    print("***Transform yaml data into dictionaries or lists***")
    data = yaml.load(file_data)
    print(data)
    print("Type:", type(data))
    return data
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "config.yaml")
get_yaml_data(yaml_path)

"""
***Get yaml file data***
# yaml key-value pairs: i.e. dictionaries in python
usr: my
psw: 123455
Type:<class 'str'>
***Transform yaml data into dictionaries or lists***
{'usr': 'my', 'psw': 123455}
Type:<class 'dict'>
"""

yaml file data as key-value pairs

(1) The contents of the yaml file are key-value pairs.

# yaml key-value pairs: i.e. dictionaries in python
usr: my
psw: 123455
s: " abc\n"

Data obtained by python after parsing the yaml file.

{'usr': 'my', 'psw': 123455, 's': ' abc\n'}

(2) The content of the yaml file is "key-value pairs’ nested in "key-value pairs"

# yaml key-value pair nesting: i.e. dictionary nested dictionaries in python
usr1:
  name: a
  psw: 123
usr2:
  name: b
  psw: 456

Data obtained by python after parsing the yaml file.

{'usr1': {'name': 'a', 'psw': 123}, 'usr2': {'name': 'b', 'psw': 456}}

(3) nested "arrays" in "key-value pairs" in the yaml file

# Nested arrays in yaml key-value pairs
usr3:
  - a
  - b
  - c
usr4:
  - b

Data obtained by python after parsing the yaml file.

{'usr3': ['a', 'b', 'c'], 'usr4': ['b']}

yaml file data as an array

(1) The contents of the yaml file are arrays

# yaml arrays
- a
- b
- 5

Data obtained by python after parsing the yaml file.

['a', 'b', 5]

(2) yaml file "array" nested in "key-value pairs"

# Nested "key-value pairs" in "arrays" in yaml
- usr1: aaa
- psw1: 111
  usr2: bbb
  psw2: 222

Data obtained by python after parsing the yaml file:

[{'usr1': 'aaa'}, {'psw1': 111, 'usr2': 'bbb', 'psw2': 222}]

Basic data types in yaml files

# Pure quantity
s_val: name              # String: {'s_val': 'name'}
spec_s_val: "name\n"    # Special string: {'spec_s_val': 'name\n'}
num_val: 31.14          # Number: {'num_val': 31.14}
bol_val: true           # Boolean value: {'bol_val': True}
nul_val: null           # null value: {'nul_val': None}
nul_val1: ~             # null value: {'nul_val1': None}
time_val: 2018-03-01t11:33:22.55-06:00     # Time value: {'time_val': datetime.datetime(2018, 3, 1, 17, 33, 22, 550000)}
date_val: 2019-01-10    # Date value: {'date_val': datetime.date(2019, 1, 10)}

Reference in yaml file

The contents of the yaml file

animal3: &animal3 fish
test: *animal3

The data read by python

{'animal3': 'fish', 'test': 'fish'}

Reading multiple yaml documents in Python

Multiple documents in one yaml file, segmented using — separations

For example: data in yaml file

# Segmenting multiple documents in a yaml file
---
animal1: dog
age: 2
---
animal2: cat
age: 3

python script to read multiple documents in a yaml file method

python to get yaml data when you need to use the load_all function to parse all the documents, and then read the data from the object

# yaml file contains more than one document, get the data in the document separately
def get_yaml_load_all(yaml_file):
    # Open yaml file
    file = open(yaml_file, 'r', encoding="utf-8")
    file_data = file.read()
    file.close()
    all_data = yaml.load_all(file_data)
    for data in all_data:
        print(data)
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "config.yaml")
get_yaml_load_all(yaml_path)
"""Results
{'animal1': 'dog', 'age': 2}
{'animal2': 'cat', 'age': 3}
"""

Python object generation yaml documentation

Direct import yaml (i.e. import yaml) generated yaml document

The yaml.dump() method does not transform the list or dictionary data into the yaml standard schema, it only generates the data into a yaml document

# Generating python objects into yaml documents
import yaml
def generate_yaml_doc(yaml_file):
    py_object = {'school': 'zhang',
                 'students': ['a', 'b']}
    file = open(yaml_file, 'w', encoding='utf-8')
    yaml.dump(py_object, file)
    file.close()
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "generate.yaml")
generate_yaml_doc(yaml_path)
"""Results
school: zhang
students: [a, b]
"""

Generate standard yaml documents using the yaml method in the ruamel module

(1) Use the yaml prerequisites in the ruamel module

  • Modules that need to be installed to use yaml: ruamel.yaml (pip3 install ruamel.yaml);
  • Imported modules: from ruamel import yaml

(2) ruamel module to generate yaml documents

def generate_yaml_doc_ruamel(yaml_file):
    from ruamel import yaml
    py_object = {'school': 'zhang',
                 'students': ['a', 'b']}
    file = open(yaml_file, 'w', encoding='utf-8')
    yaml.dump(py_object, file, Dumper=yaml.RoundTripDumper)
    file.close()
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "generate.yaml")
generate_yaml_doc_ruamel(yaml_path)
"""Results
school: zhang
students:
- a
- b
"""

(3) ruamel module reads yaml documents

# Read yaml file by from ruamel import yaml
def get_yaml_data_ruamel(yaml_file):
    from ruamel import yaml
    file = open(yaml_file, 'r', encoding='utf-8')
    data = yaml.load(file.read(), Loader=yaml.Loader)
    file.close()
    print(data)
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "dict_config.yaml")
get_yaml_data_ruamel(yaml_path)
Like(1)
Python OS Module
os.accessos.chdiros.chflagos.chmodos.chownos.chrootos.closeos.closerangeos.dupos.dup2os.fchdiros.fchmodos.fchownos.fdatasyncos.fdopenos.fpathconfos.fstatos.fstatvfsos.fsyncos.ftruncateos.getcwdos.getcwdbos.isattyos.lchflagsos.lchmodos.lchownos.linkos.listdiros.lseekos.lstatos.majoros.makedevos.makedirsos.minoros.mkdiros.mkfifoos.mknodos.openos.openptyos.pathconfos.pipeos.popenos.reados.readlinkos.removeos.removedirsos.renameos.renamesos.rmdiros.statos.stat_float_timesos.statvfsos.symlink()os.tcgetpgrpos.tcsetpgrpos.ttynameos.unlinkos.utimeos.walk()os.write()os.pardir
Python Module
Python yaml modulePython argparse module
Python Tutorials
Python with UsageOs.getenv() in PythonSubtract String Lists in PythonBuilding Physical Projects with Python on the Raspberry PiIntroduction to PyOpenGL in PythonIntroduction to the pywhatkit LibraryLee Algorithm in PythonNew Features and Fixes in Python 3.11Pendulum Library in PythonPython doctest Module | Document and Test CodePython Site Connectivity Checker ProjectPython with Qt Designer: Quicker GUI Application DevelopmentRegular Expressions in PythonShould We Update the Latest Version of Python Bugfix?Some Advance Ways to Use Python DictionariesString Manipulation in Python: A Comprehensive GuideSubsets in PythonUtilize Python and Rich to Create a Wordle CloneValidating Bank Account Number Using Regular Expressions in PythonCollections in PythonCreate a GUI to extract information from VIN number Using PythonCreate XML Documents Using PythonCreating a Basic hardcoded ChatBot using Python -NLTKCreating a SQLite Database from CSV with PythonHow can I make sense of the else clause of Python loops?
Python String Module
Python String capitalize()Python String count()Python String center()Python String expandtabs()Python String index()Python String isalnum()Python String endswith()Python String encode()Python String find()Python String decode()Python String isalpha()Python String isdigit()Python String islower()Python String isnumeric()Python String isspace()Python String istitle()Python String isupper()Python String join()Python String len()Python String ljust()Python String lower()Python String lstrip()Python String maketrans()Python String max()Python String min()Python String replace()Python String rfind()Python String rindex()Python String rjust()Python String rstrip()Python String isdecimal()Python String split()Python String splitlines()Python String startswith()Python String strip() MethodPython String swapcase()Python String title()Python String translate()Python String upper()Python String zfill()
Python Math Module
Python Math exp()Python Math ceil()Python Math fabs()Python Math floor()Python Math log10()Python Math log()Python Math modf()Python Math pow()Python Math sqrt()Python Math acos() MethodPython Math asin() MethodPython Math atan() MethodPython Math atan2() MethodPython Math cos() MethodPython Math degrees() MethodPython Math hypot() MethodPython Math radians() MethodPython Math sin() MethodPython Math tan() Method
Python Random Module
Python random choice() MethodPython random random() MethodPython random randrange() MethodPython random seed() MethodPython random shuffle() MethodPython random uniform() Method
Python List Module
Python List min() MethodPython List len() MethodPython List list() MethodPython List max() Method
Python Questions
How to Check if a Dictionary is Empty in Python?How to Validate Email Address in Python with Regular ExpressionDifference Between Python and Gator AIDifference Between Tornado and TyphoonHow to Create a Null Matrix in PythonHow to Install Python on UbuntuHow to Add a column to a DataFrame in PythonHow to Add in PythonHow to Add to a Set in PythonHow to Append to a Dictionary in PythonHow to Change Python VersionHow to Check if a List is Empty in PythonHow to Check if Key Exists in Dictionary PythonHow to Check if Python is InstalledHow to Comment Multiple Lines in PythonHow to Compare Strings in Python
Python Examples
Python Program to Append (key: value) Pair to DictionaryPython Program to Define a Python Class for Complex NumbersPython Program to Implementation of Kruskal's AlgorithmPython Program to Add Elements to a DictionaryPython Program to Calculate the Symmetric Difference Between Two ListsPython Program to Check if Two Sets Are EqualPython Program to Convert List into ArrayPython Program to Create a Dictionary with a Dictionary LiteralPython Program To Find The Largest Element In A DictionaryPython Program to get first and last element from a DictionaryPython Program to Remove Null Values from a ListPython Program to Remove Null Values from a DictionaryPython Program to Replace Elements in a ListPython Program to Rotate Elements of a ListPython Program to Search an Element in a DictionaryPython Program to Print a Spiral MatrixPython Program To Add Elements To A Linked ListPython Program To Convert An Array List Into A String And ViceversaPython Program To Detect A Loop In A Linked ListPython Program To Get The Middle Element Of A Linked List In A Single IterationPython program to implement binary tree data structureCalculate the n-th discrete difference for unsigned integer arrays in PythonCalculate the n-th discrete difference in PythonCalculate the n-th discrete difference over axis 0 in PythonCalculate the n-th discrete difference over axis 1 in PythonCalculate the n-th discrete difference over given axis in PythonDifference between Data Frames and Matrices in Python PandasDifference Between Del and Remove() on Lists in PythonDifference between for loop and while loop in PythonDifference between indexing and slicing in PythonDifference Between Matrices and Arrays in Python?Difference between .pack and .configure for widgets in TkinterDifference between Python and C++Difference between Python and JavaScriptDifference between Python and LuaDifference Between range() and xrange() Functions in Python?Difference between Yield and Return in PythonWhat is the difference between arguments and parameters in Python?What is the difference between attributes and properties in python?What is the Difference between Core Python and Django Python?What is the Difference Between Freedom of Information and Information Privacy?What is the difference between Risk Acceptance and Risk Avoidance?What is the Difference Between Scala and Python?
Python3 Tutorials
Python 3 TutorialWhat is New in Python 3Python 3 - OverviewPython 3 - Environment SetupPython 3 - Basic SyntaxPython 3 - Command Line ArgumentsPython 3 - Variable TypesPython 3 - Basic OperatorsPython 3 - Arithmetic Operators ExamplePython 3 - Comparison Operators ExamplePython 3 - Assignment Operators ExamplePython 3 - Bitwise Operators ExamplePython 3 - Logical Operators ExamplePython 3 - Membership Operators ExamplePython 3 - Identity Operators ExamplePython 3 - Operators Precedence ExamplePython 3 - Decision MakingPython 3 - IF StatementPython 3 - IF...ELIF...ELSE StatementsPython 3 - Nested IF StatementsPython 3 - LoopsPython 3 - While Loop StatementsPython 3 - for Loop StatementsPython 3 - Nested Loops: A Comprehensive GuidePython 3 - break statementPython 3 - continue statementPython 3 - pass StatementPython 3 - NumbersPython 3 - Number abs() MethodPython 3 - Number ceil() MethodPython 3 - Number exp() MethodPython 3 - Number fabs() MethodPython 3 - Number floor() MethodPython 3 - Number log() MethodPython 3 - Number log10() MethodPython 3 - Number max() MethodPython 3 - Number min() MethodPython 3 - modf() MethodPython 3 - Number pow() MethodPython 3 - Number round() MethodPython 3 - Number sqrt() MethodPython 3 - Number choice() MethodPython 3 - Number randrange() MethodPython Number random() MethodPython 3 - Number seed() MethodPython 3 - Number shuffle() MethodPython 3 - Number uniform() MethodPython 3 - Number acos() MethodPython 3 - Number asin() MethodPython 3 - Number atan() MethodPython 3 - Number atan2() MethodPython 3 - Number cos() MethodPython 3 - Number hypot() MethodPython 3 - Number sin() MethodPython 3 - Number tan() MethodPython 3 - Number degrees() MethodPython 3 - Number radians() MethodPython 3 - StringsPython 3 - String capitalize() MethodPython 3 - String center() MethodPython 3 - String count() MethodPython 3 - String decode() MethodPython 3 - String encode() MethodPython 3 - String endswith() MethodPython 3 - String expandtabs() MethodPython 3 - String find() MethodPython 3 - String index() MethodPython 3 - String isalnum() MethodPython 3 - String isalpha() MethodPython 3 - String isdigit() MethodPython 3 - String islower() MethodPython 3 - String isnumeric() MethodPython 3 - String isspace() MethodPython 3 - String istitle() MethodPython 3 - String isupper() MethodPython 3 - String join() MethodPython 3 - String len() MethodPython 3 - String ljust() MethodPython 3 - String lower() MethodPython 3 - String lstrip() MethodPython 3 - String maketrans() MethodPython 3 - dictionary str() MethodPython 3 - String max() MethodPython 3 - dictionary type() MethodPython 3 - String min() MethodPython 3 - dictionary clear() MethodPython 3 - String replace() MethodPython 3 - dictionary copy() MethodPython 3 - String rfind() MethodPython 3 - dictionary fromkeys() MethodPython 3 - String rindex() MethodPython 3 - dictionary get() MethodPython 3 - String rjust() MethodPython 3 - dictionary has_key() MethodPython 3 - String rstrip() MethodPython 3 - dictionary items() MethodPython 3 - String split() MethodPython 3 - dictionary keys() MethodPython 3 - String splitlines() MethodPython 3 - Dictionary setdefault() MethodPython 3 - String startswith() MethodPython 3 - dictionary update() MethodPython 3 - String strip() MethodPython 3 - dictionary values() MethodPython 3 - String swapcase() MethodPython 3 - Date & TimePython 3 - String title() MethodPython 3 - time altzone() MethodPython 3 String translate() MethodPython 3 - time asctime() MethodPython 3 - String upper() MethodPython 3 - time clock() MethodPython 3 - String zfill() MethodPython 3 - time ctime() MethodPython 3 - String isdecimal() MethodPython 3 - time gmtime() MethodPython 3 - ListsPython 3 - time localtime() MethodPython 3 - List len() MethodPython 3 - time mktime() MethodPython 3 - List max() MethodPython 3 - time sleep() MethodPython 3 - List min() MethodPython 3 - time strftime() MethodPython 3 - List list() MethodPython 3 - time strptime() MethodPython 3 - List append() MethodPython 3 - time time() MethodPython 3 - List count() MethodPython 3 - time tzset() MethodPython 3 - List extend() MethodPython 3 - FunctionsPython 3 - List index() MethodPython 3 - ModulesPython 3 - List insert() MethodPython 3 - Files I/OPython 3 - List pop() MethodPython 3 - File MethodsPython 3 - List remove() MethodPython 3 - OS File/Directory MethodsPython 3 - List reverse() MethodPython 3 - Exceptions HandlingPython 3 - List sort() MethodPython 3 - Object OrientedPython 3 - TuplesPython 3 - Regular ExpressionsPython 3 - Tuple len() MethodPython 3 - CGI ProgrammingPython 3 - Tuple max() MethodPython 3 - MySQL Database AccessPython 3 - Tuple min() MethodPython 3 - Network ProgrammingPython 3 - Tuple tuple() MethodPython 3 - Sending Email using SMTPPython 3 - DictionaryPython 3 - Multithreaded ProgrammingPython 3 - Dictionary cmp() MethodPython 3 - XML ProcessingPython 3 - Dictionary len() MethodPython 3 - GUI Programming (Tkinter)Python 3 - Tkinter ButtonPython 3 - Tkinter CanvasPython 3 - Tkinter CheckbuttonPython 3 - Tkinter EntryPython 3 - Tkinter FramePython 3 - Tkinter LabelPython 3 - Tkinter ListboxPython 3 - Tkinter MenubuttonPython 3 - Tkinter MenuPython 3 - Tkinter MessagePython 3 - Tkinter RadiobuttonPython 3 - Tkinter ScalePython 3 - Tkinter ScrollbarPython 3 - Tkinter TextPython 3 - Tkinter ToplevelPython 3 - Tkinter SpinboxPython 3 - Tkinter PanedWindowPython 3 - Tkinter LabelFramePython 3 - Tkinter tkMessageBoxPython 3 - Tkinter DimensionsPython 3 - Tkinter ColorsPython Tkinter FontsPython 3 - Tkinter AnchorsPython 3 - Tkinter Relief stylesPython 3 - Tkinter BitmapsPython 3 - Tkinter CursorsPython 3 - Tkinter pack() MethodPython Tkinter grid() MethodPython 3 - Tkinter place() MethodPython 3 - Extension Programming with CPython 3 -File close() MethodPython 3 - File flush() MethodPython 3 - File fileno() MethodPython 3 - File isatty() MethodPython 3 - File next() MethodPython 3 - File read() MethodPython 3 - File readline() MethodPython 3 - File readlines() MethodPython 3 - File seek() MethodPython 3 - File tell() MethodPython 3 - File Truncate() MethodPython 3 - File write() MethodPython 3 - File writelines() MethodPython 3 - os.access() MethodPython 3 - os.chdir() MethodPython 3 - os.chflags() MethodPython 3 - os.chmod() MethodPython 3 - os.chown() MethodPython 3 - os.chroot() MethodPython 3 - os.close() MethodPython 3 - os.closerange() MethodPython 3 - os.dup() MethodPython 3 - os.dup2() MethodPython 3 - os.fchdir() MethodPython 3 - os.fchmod() MethodPython 3 - os.fchown() MethodPython 3 - os.fdatasync() MethodPython 3 - os.fdopen() MethodPython 3 - os.fpathconf() MethodPython 3 - os.fstat() MethodPython 3 - os.fstatvfs() MethodPython 3 - os.fsync() MethodPython 3 - os.ftruncate() MethodPython 3 - os.getcwd() MethodPython 3 - os.getcwdu() MethodPython 3 - os.isatty() MethodPython 3 - os.lchflags() MethodPython 3 - os.lchmod() MethodPython 3 - os.lchown() MethodPython 3 - os.link() MethodPython 3 - os.listdir() MethodPython 3 - os.lseek() MethodPython 3 - os.lstat() MethodPython 3 - os.major() MethodPython 3 - os.makedev() MethodPython 3 - os.makedirs() MethodPython 3 - os.minor() MethodPython 3 - os.mkdir() MethodPython 3 - os.mkfifo() MethodPython 3 - os.mknod() MethodPython 3 - os.open() MethodPython 3 - os.openpty() MethodPython 3 - os.pathconf() MethodPython 3 - os.pipe() MethodPython 3 - os.popen() MethodPython 3 - os.read() MethodPython 3 - os.readlink() MethodPython 3 - os.remove() MethodPython 3 - os.removedirs() MethodPython 3 - os.rename() MethodPython 3 - os.renames() MethodPython 3 - os.rmdir() MethodPython 3 - os.stat() MethodPython 3 - os.stat_float_times() MethodPython 3 - os.statvfs() MethodPython 3 - os.symlink() MethodPython 3 - os.tcgetpgrp() MethodPython 3 - os.tcsetpgrp() MethodPython 3 - os.tempnam() MethodPython 3 - os.tmpfile() MethodPython 3 - os.tmpnam() MethodPython 3 - os.ttyname() MethodPython 3 - os.unlink() MethodPython 3 - os.utime() MethodPython 3 - os.walk() MethodPython 3 - os.write() Method