I have some .xml files I need to convert into .txt files programmatically for a customer. There is no need to parse the xml tree to return the text values. I literally just need to change the file extension from .xml to .txt. The converted file would contain all of the xml tree including tags etc. The customer then wants to parse this later on.
So far I have:
import xml.etree.ElementTree as ET
tree = ET.parse('myfile.xml')
root = tree.getroot()
with open('myfile.txt', 'w') as f:
f.write(root)
f.close()
Which is returning an error of:
Traceback (most recent call last):
File "C:/Users/myuser/Documents/Python 3 Scripts/test.py", line 5, in <module>
f.write(root)
TypeError: write() argument must be str, not xml.etree.ElementTree.Element
What is the required fix to resolve?