yaml简介
1、yaml [ˈjæməl]: Yet Another Markup Language :另一种标记语言。yaml 是专门用来写配置文件的语言,非常简洁和强大。它实质上是一种通用的数据串行化格式。YAML 是一种非常灵活的格式,几乎是 JSON 的超集。除了支持注释、换行符分隔、多行字符串、裸字符串和更灵活的类型系统之外,YAML 也支持引用文件,以避免重复代码。
2、在自动化测试中,通常使用yaml文件来编写自动化测试用例。例如:
3、yaml基本语法规则:
- 大小写敏感
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键,只允许使用空格。
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
- #表示注释,从这个字符一直到行尾,都会被解析器忽略,这个和python的注释一样
4、yaml支持的数据结构有三种:
- 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
- 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
- 纯量(scalars):单个的、不可再分的值。字符串、布尔值、整数、浮点数、Null、时间、日期
5、python中,文件后缀名为.yml与.yaml的文件表示作用相同,即yaml文件;一般来说,最常用的使用.yml作为yaml文件后缀名。例如:
6、一个在线网站,用于校验yaml文件格式是否正确: http://www.bejson.com/validators/yaml/
安装yaml
使用pip安装pyyaml模块:
pip install pyyamlyaml文件支持的数据结构举例
1、yaml里面的键值对,也就是python里面的字典(dict)数据类型;比如python字典:
# python3.6{
"user": "admin",
"psw": "123456,
}
在yaml文件里可以这样写:
# yamluser: admin
psw: 123456
2、字典嵌套字典:
# python3.6"nb1": {
"user": "admin",
"psw": "123456,
}
yaml文件里可以这样写:
# yamlnb1:
user: admin
psw: 123456
yaml文件中的序列(list)
yaml文件里面写一个数组,需要在前面加一个‘-’符号。如下:
- admin1: 123456- admin2: 111111
- admin3: 222222
对应python里面的list数据类型:
[{'admin1': 123456}],[{'admin2': 111111}],
[{'admin3': 222222}]
注意:数字读出来的是int或float类型
yaml文件中的纯量(str)
1、int和float类型的数字
n1: 12.30对应python中的
{'n1': 12.3}2、布尔值用true和false表示
n2: truen3: false
对应python中的
{'n2': True, 'n3': False}注意:与从文本中读出来的json格式的字符串通过反序列化转为python数据类型相同。
3、None用~表示。
n4: ~对应python中的
{'n4': None}注意:从文本中读取出来的json格式的字符串是null的时候转换成python数据类型是None。
4、时间采用 ISO8601 格式
time1: 2001-12-14t21:59:43.10-05:00对应python中的
{'time1': datetime.datetime(2001, 12, 15, 2, 59, 43, 100000)}5、日期采用复合 iso8601 格式的年、月、日表示。
date1: 2017-07-31对应python中的
{'date1': datetime.date(2017, 7, 31)}6、使用两个感叹号,强制转换数据类型。
# int转strn6: !!str 123
对应python中的
{'n6': '123'}# bool值转strn7: !!str true
对应python中的
{'n7': 'true'}举例:在yaml文件写入以下内容:
n1: 12.30n2: true
n3: false
n4: ~
time1: 2018-04-18t21:59:43.10+08:00
date1: 2018-04-18
n6: !!str 123
n7: !!str true
python读取结果:
{'n1': 12.3,'n2': True,
'n3': False,
'n4': None,
'time1': datetime.datetime(2018, 4, 18, 13, 59, 43, 100000),
'date1': datetime.date(2018, 4, 18),
'n6': '123',
'n7': 'true'}
混合使用
1、list嵌套dict,在yaml里面写入如下内容:
- user: admin1psw: '123456'
- user: admin2
psw: '111111'
- user: admin3
psw: '222222'
用python读取出来的结果:
[{'user': 'admin1', 'psw': '123456'},{'user': 'admin2', 'psw': '111111'},
{'user': 'admin3', 'psw': '222222'}]
2、dict嵌套list,在yaml里面写入如下内容:
nub1:- admin1
- '123456'
nb2:
- admin2
- '111111'
nb3:
- admin3
- '222222'
用python读取出来的结果:
{'nub1': ['admin1', '123456'],'nb2': ['admin2', '111111'],
'nb3': ['admin3', '222222']}
使用python的load()方法读取yaml文件内容【反序列化】
data=yaml.load(f,Loader=yaml.FullLoader)在 yaml.load 方法中, loader 参数有四种:
①BaseLoader:载入大部分的基础YAML
②SafeLoader:载入YAML的子集,推荐在不可信的输入时使用
③FullLoader:这是默认的载入方式,载入全部YAML
④UnsafeLoader:老版本的载入方式
注意:
需要加上参数: Loader=yaml.FullLoader
否则会报错: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
如图:
举例1:
import yamldef main():
with open("./data.yaml","r") as f:
data=yaml.load(f,Loader=yaml.FullLoader)
print(data)
if __name__ == '__main__':
main()
举例2:
# coding:utf-8import yaml
import os
# 获取当前脚本所在文件夹路径
curPath = os.path.dirname(os.path.realpath(__file__))
# 获取yaml文件路径
yamlPath = os.path.join(curPath, "cfgyaml.yaml")
# open方法打开直接读出来
f = open(yamlPath, 'r', encoding='utf-8')
cfg = f.read()
print(type(cfg)) # 读出来是字符串
print(cfg)
d = yaml.load(cfg,Loader=yaml.FullLoader) # 用load方法将json字符串转换字典类型
print(d) print(type(d))
运行结果:
使用python的safe_load()方法读取yaml文件内容【反序列化】
使用 yaml.safe_load() 方法,这个只解析基本的yaml标记,用来保证代码的安全性,不过这对于平常保存数据是足够了。
源码如下:
def safe_load(stream):"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
Resolve only basic YAML tags. This is known
to be safe for untrusted input.
"""
return load(stream, SafeLoader)
def load(stream, Loader=None):
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
"""
if Loader is None:
load_warning('load')
Loader = FullLoader
loader = Loader(stream)
try:
return loader.get_single_data()
finally:
loader.dispose()
可以看到 safe_load() 方法就是在load方法中传入SafeLoader的解析器,那么yaml有些什么Loader呢?
①UnsfeLoader & LoaderThe original Loader code that could be easily exploitable by untrusted data input.
②SafeLoader:
Loads a subset of the YAML language, safely. This is recommended for loading untrusted input.
安全的加载yaml语言子集,对于加载不受信任的输入,推荐使用此种方式 yaml.safe_load()
③FullLoader:
Loads the full YAML language. Avoids arbitrary code execution. This is currently (PyYAML 5.1) the default loader called by yaml.load(input) (after issuing the warning).
加载完整的yaml语言,从上方的源码可以看出这个是loade()默认的加载方式
④BaseLoader:
Only loads the most basic YAML
只加载最基本的yaml
safe_load()方法举例:
a.yml文件:
代码如下:
import yamlfrom string import Template
def yaml_template(data: dict):
with open("a.yml", encoding="utf-8") as f:
re = Template(f.read()).substitute(data)
return yaml.load(stream=re, Loader=yaml.FullLoader)
if __name__ == '__main__':
print(yaml_template({'token': 'hdadhh21uh1283hashdhuhh2hd', 'username': 'admin', 'password': '123456'}))
运行结果:
{'method': 'get', 'url': 'http://www.baidu.com', 'headers': {'Content-Type': 'application/json', 'token': 'hdadhh21uh1283hashdhuhh2hd'}, 'data': {'username': 'admin', 'password': 123456}}使用python的dump()方法将python字典写入yaml文件【序列化】
yaml.dump(data,f,encoding='utf-8',allow_unicode=True)当data数据中有汉字时,加上: encoding='utf-8',allow_unicode=True
举例:
import osimport yaml
yaml_dict = {
"user": "general",
"country": "China",
"gender": "male",
"address": "北京"
}
yaml_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'a.yml')
with open(yaml_dir, 'w', encoding='utf-8', ) as f:
yaml.dump(yaml_dict, f, encoding='utf-8', allow_unicode=True)
运行结果:
如果在写入yaml文件不加 encoding='utf-8',allow_unicode=True 参数时,即:
import osimport yaml
yaml_dict = {
"user": "general",
"country": "China",
"gender": "male",
"address": "北京"
}
yaml_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'a.yml')
with open(yaml_dir, 'w', encoding='utf-8', ) as f:
yaml.dump(yaml_dict, f)
运行结果:
如上图,如果在写入yaml文件不加 encoding='utf-8',allow_unicode=True可以看到汉字是以unicode码写入到yaml文件当中。
在实际使用的时候可以配合,输入参数更新配置文件中的参数使用:
def merge_config(config,args):for key_1 in config.keys():
if(isinstance(config[key_1],dict)):
for key_2 in config[key_1].keys():
if(key_2) in dir(args):
config[key_1][key_2] = getattr(args,key_2)
return config
config = yaml.load(open(args.config, 'r', encoding='utf-8'),Loader=yaml.FullLoader)
config = merge_config(config,args)
去期待陌生,去拥抱惊喜。