[英]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.