Yaml如何做动态参数化
话不多说,直接上代码:
data.yaml
data222: {{rand_str()}}
demo.py
import os import jinja2 import yaml from faker import Faker def render(tpl_path, **kwargs): path, filename = os.path.split(tpl_path) return jinja2.Environment(loader=jinja2.FileSystemLoader(path or './') ).get_template(filename).render(**kwargs) # yaml 文件调用以下函数 def rand_str(): faker = Faker("zh_CN") return str(faker.phone_number()) #生成随机的手机号码 if __name__ == '__main__': r = render("data.yaml", **{"rand_str": rand_str}) print(r) print(yaml.safe_load(r))
运行结果:
data222: 18966216278 {'data222': 18966216278}
本文来自博客园,作者:他还在坚持嘛,转载请注明原文链接:他还在坚持嘛 https://www.cnblogs.com/brf-test/p/16320738.html