[英]Python: How to add a prefix to tags in an xml.etree.ElementTree
我使用python asciimathml库来解析一些asciimathml并将其转换为MathML
>>> from xml.etree.ElementTree import tostring
>>> tostring(asciimathml.parse('sqrt 2'))
'<math><mstyle><msqrt><mn>2</mn></msqrt></mstyle></math>'
唯一的麻烦是我需要带有m:
前缀的标签。 我如何更改上面的代码,所以我得到:
'<m:math><m:mstyle><m:msqrt><m:mn>2</m:mn></m:msqrt></m:mstyle></m:math>'
您可以重命名标签,并添加“ m:”前缀:
import asciimathml
from xml.etree.ElementTree import tostring
tree = asciimathml.parse('sqrt 2')
for elem in tree.getiterator():
elem.tag = 'm:' + elem.tag
print tostring(tree)
结果:
<m:math><m:mstyle><m:msqrt><m:mn>2</m:mn></m:msqrt></m:mstyle></m:math>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.