python接口自动化测试二十九:yaml配置文件的写和读(ruamel.yaml)
yaml 通常用来存储数据,类似于json
安装:pip install ruamel.yaml
建一个空的yaml文件
写入数据
import os
from ruamel import yaml
# 将字典写入到yaml
data = {
'host1': '123',
'host2': 456,
'host3': {'asd': '123'},
'host4': [12, '234', ['wer', 234]],
'host5': {'asd': '123', 'eee': [12, '234', ['wer', 234]]}
}
curpath = os.path.dirname(os.path.realpath(__file__)) # 当前脚本路径
yamlpath = os.path.join(curpath, "config.yaml") # 在当前脚本路径中,找到config.yaml文件
# 写入到yaml文件
with open(yamlpath, "w", encoding="utf-8") as f:
yaml.dump(data, f, Dumper=yaml.RoundTripDumper)
读取yaml文件
import os
from ruamel import yaml
curpath = os.path.dirname(os.path.realpath(__file__)) # 当前脚本路径
yamlpath = os.path.join(curpath, "config.yaml") # 在当前脚本路径中,找到config.yaml文件
# 读取yaml文件
data = yaml.load(open(yamlpath, "r").read(), Loader=yaml.Loader)
print(data)
print(data['host4'][1])
封装起来以后好调用
import os
from ruamel import yaml
class WRYaml:
""" yaml文件的读和写 """
def __init__(self):
""" 指定yaml文件的路径 """
self.configpath = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'config')
def read_yaml(self, yaml_file='conf.yaml'):
""" 读取yaml里面里面的数据"""
try:
with open(os.path.join(self.configpath, yaml_file), "r", encoding='utf8') as f:
return yaml.load(f, Loader=yaml.Loader)
except Exception as error:
print(f'读取yaml失败,错误如下:{error}')
return False
def write_yaml(self, data, yaml_file='conf.yaml', mode='w'):
""" 往yaml里面写入数据
yamlFile:yaml文件名
data:要写入的数据
mode:写入方式: w,覆盖写入, a,追加写入
将原数据读取出来,如果没有要加入的key,则创建一个,如果有,则执行key下面的数据修改
"""
try:
old_data = self.read_yaml(yaml_file) or {}
for data_key, data_value in data.items():
if not old_data.get(data_key):
old_data.setdefault(data_key, {})
for value_key, value_value in data_value.items():
old_data[data_key][value_key] = value_value
with open(os.path.join(self.configpath, yaml_file), mode, encoding="utf-8") as f:
yaml.dump(old_data, f, Dumper=yaml.RoundTripDumper)
return True
except Exception as error:
print(f'yaml文件写入失败,错误如下:\n{error}')
return False
if __name__ == "__main__":
wryaml = WRYaml()
# 写入数据文件
data = {
'test': {'AAA': 134511, 'BBB': 333}
}
print(wryaml.write_yaml(yaml_file='conf.yaml', data=data))
# 读取数据文件
print(wryaml.read_yaml('conf.yaml'))
讨论群:249728408