how to write pretty xml to a file python

Solutions on MaxInterview for how to write pretty xml to a file python by the best coders in the world

showing results for - "how to write pretty xml to a file python"
Nicolò
30 Apr 2020
1# Use lxml as the core library for xml manipulation. 
2from lxml import etree
3
4xml_object = etree.tostring(root,
5                            pretty_print=True,
6                            xml_declaration=True,
7                            encoding='UTF-8')
8
9with open("xmlfile.xml", "wb") as writter: # wb - write bytes mode
10    writter.write(xml_object)`
11