0

I am converting an xml file to string, then converting it back to xml. I want the original xml and the one at the end to be exactly identical. But this is not the case for two reasons: 1. the keys order is changed, 2. there is a space at the end before the last slash:

# original xml:
<W Duration="180" PowerLow="0.45449999" PowerHigh="0.75449997"/>

# after str conversion:
<W Duration="180" PowerHigh="0.75449997" PowerLow="0.45449999" />

Code:

# read xml and convert to str
tree = ET.parse(xml_file_path)
root = tree.getroot()
xmlstr = ET.tostring(root, encoding='utf8', method='xml')

# str > xml and save to file
tree = ET.ElementTree(ET.fromstring(xmlstr))
filename = os.path.join(TMP_DIR, next(tempfile._get_candidate_names()) + '.xml' )
tree.write(open(filename, 'w'), encoding='unicode')

I need the two files to be exactly identical, how can I do that?

Thanks!

  • Regarding the order of attributes, see https://stackoverflow.com/q/2741480/407651 (if you use Python 3.8, the original attribute order will be preserved automatically). – mzjn Jul 08 '20 at 09:05
  • Regarding the extra space, see https://stackoverflow.com/q/55959475/407651. I would recommend using lxml, as that will fix both your issues. – mzjn Jul 08 '20 at 09:16

0 Answers0