首页 > 解决方案 > 在 Python 中使用解码进行 XML 解析

问题描述

我试图阅读的 XML 文件以 b' 开头。

不知道如何处理解码问题?

标签: python-3.xxml-parsingdata-wranglingxmltocsv

解决方案


所以你可以试试这个,但这会返回一个元素实例

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)

另一种方法是读取文件并将其完全转换为字符串文件,然后再次重新读取,这将返回一个 ElementTree 实例。您可以使用以下方法实现此目的:

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)

推荐阅读