XML 在 Python 中使用解码进行解析
XML Parsing with Decode in Python
我正在尝试读取的 XML 文件以 b' 开头。
不确定如何处理解码问题?
所以你可以试试这个,但是这个returns一个元素实例
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)
另一种方法是读取文件并将其完全转换为字符串文件,然后再次重新读取,这 returns 一个 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)
我正在尝试读取的 XML 文件以 b' 开头。
不确定如何处理解码问题?
所以你可以试试这个,但是这个returns一个元素实例
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)
另一种方法是读取文件并将其完全转换为字符串文件,然后再次重新读取,这 returns 一个 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)