Skip to content Skip to sidebar Skip to footer

How To Get Xml Output In A File With New Line Using Python Xml.etree?

I am generating xml file using 'from xml.etree import ElementTree' and placing the generated output in to a new file 'test.xml'. The output is getting placed inside the test.xml bu

Solution 1:

Why don't you use the return value of the prettify? Writing the return value of the function will solve your problem.

...
top = Element('my_document')
comment = Comment('Practising')
top.append(comment)
child = SubElement(top, 'my_information')
childs = SubElement(child,'my_name')
childs.text = 'This child contains text.'
with open("test.xml", 'w') as f:  # <--
    f.write(prettify(top))        # <--

Post a Comment for "How To Get Xml Output In A File With New Line Using Python Xml.etree?"