0

The XML File I'm trying to read starts with b' .

Not sure how to handle the decode issue?

Emma
  • 49
  • 5
  • Hi there, I am guessing the XML file you posted is not the complete XML file right? – Fading Origami Nov 07 '20 at 05:57
  • This post may help https://stackoverflow.com/a/21698118/10191959 – Fading Origami Nov 07 '20 at 05:58
  • @FadingOrigami No not complete. Just a snippet. – Emma Nov 07 '20 at 06:02
  • @FadingOrigami File "", line unknown ParseError: not well-formed (invalid token): line 1, column 1 I get this error. – Emma Nov 07 '20 at 06:05
  • I guess that there is a `'` character at the end of the XML file. It seems like the XML content is given as a bytes literal (see https://stackoverflow.com/q/6269765/407651). Try removing the leading `b'` and the trailing `'` from the file. – mzjn Nov 07 '20 at 06:43

1 Answers1

0

so you can try this, but this returns an Element Instance

import ast
import xml.etree.ElementTree as etree


tree = None 

with open("property.xml", "r") as xml_file:
     f = xml_file.read()
     
     # convert string representation of bytes back to bytes
     raw_xml_bytes= ast.literal_eval(f)
     
     # read XML from raw bytes
     tree = etree.fromstring(raw_xml_bytes)

Another way is to read the file and convert it fully to a string file and then reread it again, this returns an ElementTree instance. You can achieve this using the following:

tree = None

with open("property.xml", "r") as xml_file:
    f = xml_file.read()
     
    # convert string representation of bytes back to bytes
    raw_xml_bytes= ast.literal_eval(f)

# save the converted string version of the XML file
with open('output.xml', 'w') as file_obj:
    file_obj.write(raw_xml_bytes.decode())

# read saved XML file 
with open('output.xml', 'r') as xml_file:
    tree = etree.parse(f)