I have a bytes object containing a utf-8 encoded xml file(say, file1). I need to save this file to directory as an xml file so I convert it into an ElementTree with the following code:
import xml.etree.ElementTree as ET
tree = ET.ElementTree(ET.fromstring(file1))
I expect when I convert this back using the following line to also be utf-8 encoded and to be entirely equal to file1.
file2 = ET.tostring(tree.getroot(), encoding='utf-8', method='xml')
To be clear, I expect file1 == file2 to return True, yet it returns False. Looking at the bytes objects, I can see that file1 starts with the following line yet this line is missing in file2.
b'<?xml version="1.0" encoding="UTF-8"?> #file1
Any ideas on what I'm missing?