Python 中是否有用于纯文本文件的本机模板系统? [英] Is there a native templating system for plain text files in Python?

查看:37
本文介绍了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.

作为我想要实现的简单示例,我们有变量 titlesubtitlelist

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆