Is there a native templating system for plain text files in Python?

后端 未结 4 610
盖世英雄少女心
盖世英雄少女心 2020-11-30 18:20

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 li

4条回答
  •  生来不讨喜
    2020-11-30 19:23

    If your prefer to use something shipped with the standard library, take a look at the format string syntax. By default it is not able to format lists like in your output example, but you can handle this with a custom Formatter which overrides the convert_field method.

    Supposed your custom formatter cf uses the conversion code l to format lists, this should produce your given example output:

    cf.format("{title}\n{subtitle}\n\n{list!l}", title=title, subtitle=sibtitle, list=list)
    

    Alternatively you could preformat your list using "\n".join(list) and then pass this to your normal template string.

提交回复
热议问题