I have created a xml file using xml.etree.ElementTree in python. I then use
tree.write(filename, "UTF-8")
to write out the document to a file.
But when I open filename using a text editor, there are no newlines between the tags. Everything is one big line
How can I write out the document in a "pretty printed" format so that there are new lines (and hopefully indentations etc) between all the xml tags?
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
)