解析YAML文件并替换为模板

pmv*_*pmv 7 python templates yaml

我在配置文件夹中有一堆YAML文件,在模板文件夹中有一堆模板.是否有一个简单的单行或函数,我可以用来解析YAML文件并替换模板文件来生成可执行脚本

输入:
config文件夹:config/yaml1,config/yaml2,config/yaml3..
模板:template/template1,template/template2,template3.

输出
scripts/script1,script2,script3

脚本数量=模板数量

有两种类型的模板

一个是直接替代的例子

YAML1:
    Titles:4
    SubTitles:10
Template1:
Number of Titles {Titles} where as Number of Subtitles is {SubTitles}
Run Code Online (Sandbox Code Playgroud)

其他模板是嵌套的.基本上模板需要基于YAML循环示例:

YAML2:
    Book: "The Choice of using Choice"
        Author: "Unknown1"
    Book: "Chasing Choices"
        Author:"Known2"
Template2
Here are all the Books with Author Info
The author of the {Book} is {Author}
Run Code Online (Sandbox Code Playgroud)

Ant*_*hon 1

YAML 不了解模板的任何信息,并且您不指定需要更新哪种模板。但是,如果您使用的模板语言假设其替换值来自 Python 字典中的键值对,那么您可以在 YAML 文件中指定顶级映射,加载该映射(它将被构造为 Python dict),然后将其输入模板引擎。

您仍然需要遍历文件,因此几行代码比一行代码实现上述内容更有可能。