YAML Ain’t Markup Language
,它不是标记语言,是一种轻量级数据交换格式;Python
中的PyYAML
;#
号;# 基本数据类型
str: "Hello World" # 字符串类型
int: 123 # 整数类型
float: 3.14 # 浮点数类型
bool: true # 布尔类型
null: null # 空值类型
# 数组类型
array:
- item1 # 元素1
- item2 # 元素2
- item3 # 元素3
* item1
* item2
* item3
input_des: |
My name
is
noamanelson.
output_d: >
My name
is
noamanelson.
Project: autotest
URl: xxx
env:
test: yyy
pro: zzz
mysql:
port: 8888
username: test1
password: 123456
# 请求
method: POST
path: /login
params:
username: NoamaNelson
password: 123456
# 响应
status: 200
mes: success
token: fdshfsdhfsdfiereiopojfdjfjd
body:
- userid: 1
name: NoamaNelson
Kubernetes
、Docker
;SaltStack
。Python
:pyyaml
库;Ruby
:psych
库;Java
:snakeyaml
库;JavaScript
:js-yaml
库。pip install pyyaml
config.yaml
文件,如下:# test
# 基本数据类型
str: "Hello World" # 字符串类型
int: 123 # 整数类型
float: 3.14 # 浮点数类型
bool: true # 布尔类型
null: null # 空值类型
# 数组类型
array:
- item1 # 元素1
- item2 # 元素2
- item3 # 元素3
# 映射类型
map:
key1: value1 # 键值对1
key2: value2 # 键值对2
key3: value3 # 键值对3
input_des: |
My name
is
noamanelson.
output_d: >
My name
is
noamanelson.
Project: autotest
URl: xxx
env:
test: yyy
pro: zzz
Mysql:
port: 8888
username: test1
password: 123456
# 请求
method: POST
path: /login
params:
username: NoamaNelson
password: 123456
# 响应
status: 200
mes: success
body:
- userid: 1
name: NoamaNelson
yaml_read.py
,代码如下:# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/7/27
# 文件名称:yaml_read.py
# 作用:yaml读取
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import yaml
with open("config.yaml") as f:
yaml_obj = yaml.load(f, Loader=yaml.Loader)
print(yaml_obj)
{'str': 'Hello World',
'int': 123,
'float': 3.14,
'bool': True,
None: None,
'array': ['item1', 'item2', 'item3'],
'map': {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'},
'input_des': 'My name\nis\nnoamanelson.\n',
'output_d': 'My name is noamanelson.\n',
'Project': 'autotest',
'URl': 'xxx',
'env': {'test': 'yyy', 'pro': 'zzz'},
'Mysql': {'port': 8888, 'username': 'test1', 'password': 123456},
'method': 'POST',
'path': '/login',
'params': {'username': 'NoamaNelson', 'password': 123456},
'status': 200,
'mes': 'success',
'body': [{'userid': 1, 'name': 'NoamaNelson'}]}
Python
字典转换为YAML
格式;dict_data = {'key1': 'value1', 'key2': {'name': 'xiaoming'}, 'key3': [{"name1": "xiaozhang"}]}
yaml_data = yaml.dump(dict_data)
print(yaml_data)
key1: value1
key2:
name: xiaoming
key3:
- name1: xiaozhang
YAML
格式转换为Python
字典;dict_data = {'key1': 'value1', 'key2': {'name': 'xiaoming'}, 'key3': [{"name1": "xiaozhang"}]}
yaml_data = yaml.dump(dict_data)
print(yaml_data)
yaml_data1 = 'key1: value1\nkey2:\n name: xiaoli\n'
data = yaml.load(yaml_data1, Loader=yaml.FullLoader)
data1 = yaml.load(yaml_data, Loader=yaml.FullLoader)
print(data)
print(data1)
key1: value1
key2:
name: xiaoming
key3:
- name1: xiaozhang
{'key1': 'value1', 'key2': {'name': 'xiaoli'}}
{'key1': 'value1', 'key2': {'name': 'xiaoming'}, 'key3': [{'name1': 'xiaozhang'}]}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有