0

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

    b'<?xml version="1.0" encoding="UTF-8" ?><root><property_id type="dict"><n53987 type="int">54522</n53987><n65731 type="int">66266</n65731><n44322 type="int">44857</n44322><n11633 type="int">12148</n11633><n28192 type="int">28727</n28192><n69053 type="int">69588</n69053><n26529 type="int">27064</n26529><n4844 type="int">4865</n4844><n7625 type="int">7646</n7625><n54697 type="int">55232</n54697><n6210 type="int">6231</n6210><n26710 type="int">27245</n26710><n57915 type="int">58450</n57915

import xml.etree.ElementTree as etree    
tree = etree.decode("UTF-8").parse("./property.xml")  

How can I decode this file? And read the dict type afterwards?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Emma
  • 49
  • 1
  • 6
  • 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

2 Answers2

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)
Fading Origami
  • 203
  • 1
  • 8
0

Opening and reading an xml file will return data of type bytes, which has a .decode() method (cf. https://docs.python.org/3/library/stdtypes.html#bytes.decode). You can do the following, using the appropriate encoding name:

my_xml_text = xml_file.read().decode('utf-8')
Tom
  • 11
  • 2