导入开始时带有编码器元素的xml文件(使用lxml)

2024-09-28 20:59:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件trace.xml。该文件的第一行是<?xml version="1.0" encoding="UTF-8"?>。我尝试使用以下命令读取数据:

with open('trace.xml') as fobj:
xml=fobj.read()
root = etree.fromstring(xml)

但是,这会产生以下错误:ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration. 我假设这是因为我上面提到的第一个标签。有没有办法解决这个问题?如果有任何帮助,我们将不胜感激


Tags: 文件命令readversionaswithtraceroot