0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonテンプレートエンジンempy

Last updated at Posted at 2020-10-04

empyのインストール

$ pip install empy

サンプル

テンプレートファイル

source.xml.tmplate
<?xml version="1.0" ?>
<sample>
@[if options['output_aaa']]@
  <aaa>
    <value>@(my_value)</value>
  </aaa>
@[end if]@
</sample>

実行ファイル

expand.py
import em

if __name__ == '__main__':
    template_data = {}
    template_data['options'] = {'output_aaa': True}
    template_data['my_value'] = 10
    with open('sample.xml.template', 'r') as f:
        data = f.read()
        expanded = em.expand(data, template_data)
    with open('sample.xml', 'w') as f:
        f.write(expanded)

ファイル生成

$ python expand.py
$ cat sample.xml
<?xml version="1.0" ?>
<sample>
  <aaa>
    <value>10</value>
  </aaa>
</sample>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?