In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to read and store yaml files in Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
YAML is a recursive abbreviation for "YAML Ain't a Markup Language" (YAML is not a markup language). When the language was developed, YAML actually meant "Yet Another Markup Language" (still a markup language).
The syntax of YAML is similar to that of other high-level languages and can simply express data forms such as lists, hash tables, scalars, and so on. It uses blank indentation and a lot of appearance-dependent features, and is particularly suitable for expressing or editing data structures, various configuration files, dump debugging content, and file outlines (for example, many email header formats are very close to YAML).
Basic grammar
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
Data type
YAML supports the following data types:
Object: a collection of key-value pairs, also known as mapping / hashes / dictionary (dictionary)
Array: a group of values arranged in order, also known as sequence / list
Scalar (scalars): a single, non-separable value
This is the end of the brief introduction to yaml. Today, you need to use Python to read / store yml files. How much nonsense you can fill up depends on the specific operation:
#! usr/bin/env python# encoding:utf-8from _ _ future__ import division "_ _ Author__: Yishui Cold City function: yaml operation"import sysimport yaml def write2Yaml (data, save_path=" test.yaml "):" Storage yaml files "" with open (save_path, "w") as f: yaml.dump (data) F) def loadData (data= "config.yaml"): "load yaml file"with open (data," r ") as f: content = f.read () yamlData = yaml.load (content) print (" yamlData_type: ", type (yamlData)) print (" yamlData: " YamlData) return yamlData if _ _ name__ = "_ _ main__": data = {"kind": "SeldonDeployment", "spec": {"name": "test-deployment", "predictors": [{"graph": {"endpoint": {"type": "REST"} "type": "MODEL", "name": "step_one", "children": {"endpoint": {"type": "REST"}, "type": "MODEL" "name": "step_two", "children": {"endpoint": {"type": "REST"}, "type": "MODEL", "name": "step_three" "children": [],} "componentSpecs": [{"spec": {"containers": [{"image": "seldonio/step_one:1.0" "name": "step_one",}, {"image": "seldonio/step_two:1.0" "name": "step_two",}, {"image": "seldonio/step_three:1.0" "name": "step_three",},]}], "name": "example" "replicas": 1,}],}, apiVersion: "machinelearning.seldon.io/v1alpha2", "metadata": {"name": "seldon-model"},} write2Yaml (data, save_path= "test.yaml") yamlData = loadData (data= "test.yaml") print (yamlData = = data)
The test.yaml files used for the above tests are as follows:
ApiVersion: machinelearning.seldon.io/v1alpha2kind: SeldonDeploymentmetadata: name: seldon-modelspec: name: test-deployment predictors:-componentSpecs:-spec: containers:-image: seldonio/step_one:1.0 name: step_one-image: seldonio/step_two:1.0 name: step_two-image: seldonio/step_three:1.0 name: step_three graph: Children: [] endpoint: type: REST name: step_three type: MODEL endpoint: type: REST name: step_two type: MODEL endpoint: type: REST name: step_one type: MODEL name: example replicas: 1
In the above code, you can see that the suffixes of the yaml files I operate are all written as yaml, but it is also possible to write them as yml. As follows:
This is the end of the article on "how Python reads and stores yaml files". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.