yanl 文件应该不能放函数,可以在替换的时候使用函数:
account: $account
name: $name
from string import Template
import yaml
import random
from faker import Faker
fake = Faker(locale='zh_CN')
with open("login.yaml", encoding='utf-8') as fp:
read_yml_str = fp.read()
tempTemplate1 = Template(read_yml_str)
c = tempTemplate1.safe_substitute({"account": random.randint(2, 49), "name": fake.name()})
print(c)
yaml_data = yaml.safe_load(c)
print(yaml_data)
这篇文章件介绍了 yaml 文件如何参数化
https://www.cnblogs.com/yoyoketang/p/14119435.html