import yaml
def write_yaml(data: dict, file: str):
"""
写入yaml文件
:param data: 需要写入的数据
:param file: 需要写入的文件路径
:return: Nome
"""
file = open(file, 'a+', encoding='utf-8')
yaml.dump(data, file, allow_unicode=True)
file.close()
def read_yaml(file: str) -> dict:
"""
读取yaml文件
:param file:要读取的yaml文件路径
:return: 返回字典或者数组
"""
with open(file, "r", encoding='utf-8') as file:
parameter = yaml.load(file.read(), Loader=yaml.FullLoader)
return parameter
if __name__ == "__main__":
data_1 = [{'casename': 'casefailone', 'msg': '测试失败', 'user': 'name', 'user_password': 123456},
{'casename': 'casesuccess', 'msg': '测试成功', 'user': 'shibai', 'user_password': 123456}]
write_yaml(data_1, './data_1.yaml')
data_2 = {'casename': 'casefailone', 'msg': '测试失败', 'user': 'name', 'user_password': 123456}
write_yaml(data_2, './data_2.yaml')
print(read_yaml('./data_1.yaml'))
print(read_yaml('./data_2.yaml'))
yaml的实际使用
最新推荐文章于 2024-12-13 08:56:22 发布