$ pip install empy

xml: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 expane.py
$ cat sample.xml
<?xml version="1.0" ?>
<sample>
  <aaa>
    <value>10</value>
  </aaa>
</sample>

Recommended Posts