更新时间:2023-11-07 12:20:58
我认为最简单的解决方案是切换到 lxml 图书馆.在大多数情况下,您只需将导入从 import xml.etree.ElementTree as etree
更改为 from lxml import etree
或类似.
The easiest solution I think is switching to the lxml library. In most circumstances you can just change your import from import xml.etree.ElementTree as etree
to from lxml import etree
or similar.
然后您可以在序列化时使用 pretty_print
选项:
You can then use the pretty_print
option when serializing:
tree.write(filename, pretty_print=True)
(也可在 etree.tostring
上找到)