Emma
Emma

Reputation: 49

XML Parsing with Decode in Python

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?

Upvotes: 0

Views: 4973

Answers (2)

Tom
Tom

Reputation: 11

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')

Upvotes: 0

Fading Origami
Fading Origami

Reputation: 203

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)

Upvotes: 0

Related Questions