Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use yaml File in python

Shulou Source: shulou.com Published: 2022-06-01 02:51:32 09月29日 Update

This article mainly introduces the python yaml file how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this python yaml file how to use the article will have a harvest, let's take a look at it.

Brief introduction of yaml

YAML is a markup language that uses blank indentation and a large number of appearance-dependent features. It is especially suitable for expressing or editing data structures, various configuration files, dumping debugging content, and file outlines.

Case sensitive

Use indentation to represent hierarchical relationships

Indentation does not allow tab, only spaces are allowed

The number of indented spaces does not matter, as long as the elements at the same level are aligned to the left

"#" indicates a comment

Yaml array

The yaml voice array is simple, using the dash'- "as the opening flag of the array.

One-dimensional array

The function of import yaml# 's three double quotes is to wrap characters without the newline character y = "- 111-222-333" # y equals y1y1 = "- 111-222-333" config = yaml.safe_load (y) config

Out: [111, 222, 333]

Two-dimensional array

Two-dimensional arrays are based on indentation, and more dimensional arrays make the same sense, as shown in the following code:

Import yamly = ""-- 111,222,333 "config = yaml.safe_load (y) config

Out: [[111, 222, 333], [111, 222, 333]]

Yaml dictionary

Simple dictionary form

The mode of the yaml dictionary is in the form of Key: value, with colons and spaces in the middle.

Import yamly = "0": 1 "1": 2 "2": 3 "config = yaml.safe_load (y) config

Out: {'0 ": 1,'1": 2,'2 ": 3}

Complex dictionary form

In the following example, we use a nested dictionary in which the value value is another dictionary.

Import yamly1 = "" object1: "key1": 0 "key2": 1object2: "key1": 0 "key2": 1 "config = yaml.safe_load (y1) config

Out: {'object1 ": {' key1": 0, 'key2 ": 1},' object2": {'key1 ": 0,' key2": 1}}

Embedded dictionary in yaml complex structure array

Import yamly1 = "-" key1 ": 0" key2 ": 1 -" key1 ": 0" key2 ": 1" config = yaml.safe_load (y1) config

Out: [{'key1 ": 0,' key2": 1}, {'key1 ": 0,' key2": 1}]

Embed an array in the dictionary

Import yamly1 = "object1:-111-222object2:-111222" config = yaml.safe_load (y1) config

Out: {'object1 ": [111,222],' object2": [111,222]}

Yaml read and write

Contents of YAML.yml file:

# yaml file reads import yamlwith open ("YAML.yml", "r") as f: config = yaml.safe_load (f) config

Out: [0, 111, 222]

# yaml writes to the file config = [1J 2je 3] with open ("YAML1.yml", "w") as f: yaml.dump (config,f)

Contents of YAML1.yml file:

This is the end of the article on "how to use yaml files in python". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use yaml files in python". If you want to learn more, you are welcome to follow the industry information channel.

Tags: File dictionary array content form knowledge space complexity identity that is hierarchy more article structure facet two-dimensional important three code value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker MySQL Apple macOS