inserting newlines in xml file generated via xml.etree.ElementTree in python

前端 未结 5 1633
北海茫月
北海茫月 2020-12-02 17:12

I have created a xml file using xml.etree.ElementTree in python. I then use

tree.write(filename, "UTF-8") 

to write out the documen

5条回答
  •  悲哀的现实
    2020-12-02 17:41

    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.

    You can then use the pretty_print option when serializing:

    tree.write(filename, pretty_print=True)
    

    (also available on etree.tostring)

提交回复
热议问题