Python 中是否有用于纯文本文件的本机模板系统? [英] Is there a native templating system for plain text files in Python?
问题描述
我正在寻找用于将输出格式化为简单文本的 Python 技术或模板系统.我需要的是它能够遍历多个列表或字典.如果我能够将模板定义到单独的文件(如 output.templ)而不是将其硬编码到源代码中,那就太好了.
I am looking for either technique or templating system for Python for formatting output to simple text. What I require is that it will be able to iterate through multiple lists or dicts. It would be nice if I would be able to define template into separate file (like output.templ) instead of hardcoding it into source code.
作为我想要实现的简单示例,我们有变量 title
、subtitle
和 list
As simple example what I want to achieve, we have variables title
, subtitle
and list
title = 'foo'
subtitle = 'bar'
list = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
并通过模板运行,输出将如下所示:
And running throught a template, output would look like this:
Foo
Bar
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
如何做到这一点?谢谢.
How to do this? Thank you.
推荐答案
python 的模板引擎相当多:Jinja、猎豹、Genshi 等.你不会犯任何错误.
There are quite a number of template engines for python: Jinja, Cheetah, Genshi etc. You won't make a mistake with any of them.
这篇关于Python 中是否有用于纯文本文件的本机模板系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!