博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 利用模板文件生成配置文件
阅读量:6982 次
发布时间:2019-06-27

本文共 473 字,大约阅读时间需要 1 分钟。

hot3.png

gen.py:

__author__ = 'fuhan'

from jinja2 import Template

a={'name':'a'}

b={'name':'b'}

mode_dict = {

    'a':a,

    'b':b

}

def gen_config(tplt_file, mode='a'):

    with open(tplt_file, 'r') as r:

        tplt = Template(r.read())

    config = mode_dict[mode]

    res = tplt.render(config=config)

    save_file = tplt_file.replace('.temp','')

    with open(save_file, 'w') as w:

        w.write(res)

gen_config('setting.py.temp')

setting.py.temp:

name={

{config.name}}

转载于:https://my.oschina.net/u/2367514/blog/596566

你可能感兴趣的文章
Maven构建springMVC+spring+MyBatis项目
查看>>
猴子选大王
查看>>
3249 搭积木
查看>>
POJ2749:Building roads——题解
查看>>
[SpringMVC]定义多个前缀映射的问题
查看>>
高中时的口头禅
查看>>
C++ 虚函数表解析
查看>>
[SCOI2009]windy数
查看>>
Struts2--Action属性接收参数
查看>>
2012年科技新闻背后的大数字
查看>>
报价单内,同一物料只允许一条行价格记录
查看>>
leetcode 283. Move Zeroes
查看>>
自己的php函数库
查看>>
HDU Problem 1599 find the mincost route 【Floyd求最小环】
查看>>
HDU2017多校联合 contest 1
查看>>
基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理(转载自:http://www.cnblogs.com/wuhuacong/p/4175266.html)...
查看>>
range
查看>>
[Noi2002]Savage 题解
查看>>
特征选择, 经典三刀(转)
查看>>
【Python3爬虫】自动查询天气并实现语音播报
查看>>