雑多な技術系メモ

自分用のメモ。内容は保証しません。よろしくお願いします。

pythonのElementTree(xmlファイル)で編集して、きれいに(改行をうまくして)保存する方法

サンプルコード
from xml.dom import minidom

省略

xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent="   ")
with open("xml.xml", "w") as f:
    f.write(xmlstr)

参考

https://stackoverflow.com/questions/28813876/how-do-i-get-pythons-elementtree-to-pretty-print-to-an-xml-file