how to write pretty xml to a file python
# Use lxml as the core library for xml manipulation.
from lxml import etree
xml_object = etree.tostring(root,
pretty_print=True,
xml_declaration=True,
encoding='UTF-8')
with open("xmlfile.xml", "wb") as writter: # wb - write bytes mode
writter.write(xml_object)`