When setting up a cloud-native environment such as kubernetes, yaml format files are often read. I thought it would be a good idea to use yaml without knowing it well, so this time I actually studied while moving my hands.
From yaml official website 「“YAML Ain’t Markup Language” 」
It is a way of writing a data structure represented by a recurrence acronym meaning "yaml is not a markup language".
YAML has the following seven design goals.
When dealing with yaml data, there are load and dump. load is the flow in which the program understands yaml data, while dump is the flow in which the program converts it to yaml data. In other words, it's okay if you understand that the yaml input is "load" and the yaml output is "dump" when viewed from the app. From the official website "3.1 Processing Overview"
Actually move your hands and think. This time, I will actually handle yaml using python3 as a program language.
There is a package called pyyaml to use yaml with python.
Install using pip.
pip install pyyaml
Write and check the code for load that reads yaml and dump that outputs yaml. The environment I tried is as follows.
Create a yaml file (test.yaml) as follows.
test.yaml
env:
python:3.7.3
pyYAML:5.3.1
Get the version information written in yaml with python.
Create python code (data-reader.py) to read yaml in the same folder as test.yaml.
data-reader.py
#!/usr/bin/env python3
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
with open('test.yaml', 'r') as yml:
config = load(yml, Loader=Loader)
print("#### data_chcek ####")
print(config)
print("####################")
print("software/package name:Obtained version")
print("python: {}".format(config['env']['python']))
print("pyYAML: {}".format(config['env']['pyYAML']))
I opened the terminal and ran the python code as below. It was confirmed that the data acquired from yaml was converted into a multi-layered dictionary type and the desired version information could be retrieved.
$ python3 data-reader.py
#### data_chcek ####
{'env': {'python': '3.7.3', 'pyYAML': '5.3.1'}}
####################
software/package name:Obtained version
python: 3.7.3
pyYAML: 5.3.1
Create dictionary type data as a sample, convert the created data to yaml format and output it. I created python code (data-writer.py) as follows.
data-writer.py
#!/usr/bin/env python3
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
# making data for yaml
item_list = ['apple', 'banana', 'orange']
amounts = [2, 3, 4]
sample_data = {item:value for item, value in zip(item_list, amounts)}
# data output to yaml
output = dump(sample_data, Dumper=Dumper)
with open("output_file.yaml", 'w') as yml:
yml.write(output)
Open terminal and run the python code as below. As a result of execution, it was confirmed that the dictionary type data created in the sample was converted to yaml format.
$ python3 data-writer.py
#### data_check ####
apple: 2
banana: 3
orange: 4
####################
output to output_file.yaml
You can check the contents of the output file below.
$ cat output_file.yaml
apple: 2
banana: 3
orange: 4
About yaml, I read the document and confirmed the behavior while actually writing the code. In the future, I would like to deepen my knowledge about yaml while paying attention to both load and dump.
Recommended Posts