Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use yaml File in python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report