I have an element of a tree that I want to write to a file, but you can only write strings to a file and I can't seem to convert it to a string. I tried the following:
elem = tree.find('Element', {})
file = open(filepath, "w+")
file.write(prod.text)
file.close()
Which gives the following error: TypeError: write() argument must be str, not None
even though elem is not None
My other approach was:
elem = tree.find('Element', {})
prod.write(filepath, pretty_print=True)
file.close()
Which gave: AttributeError: 'lxml.etree._Element' object has no attribute 'write'
Any ideas how to get it to work?
source https://stackoverflow.com/questions/72396880/how-to-write-xml-etree-element-to-a-file-in-python
Comments
Post a Comment