lxml errors out with TypeError: Invalid input object: list

lxml errors out with TypeError: Invalid input object: list

我正在尝试解析一个名为 sample.xml 的 xml 文件,内容如下。

<?xml version="1.0" encoding="UTF-8"?><gudid xmlns="http://www.fda.gov/cdrh/gudid" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://www.fda.gov/cdrh/gudid gudid.xsd">
<device xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.fda.gov/cdrh/gudid">
  <publicDeviceRecordKey>7c36b446-020c-44ab-9ce7-a85387467e0f</publicDeviceRecordKey>
  <publicVersionStatus>New</publicVersionStatus>
  <deviceRecordStatus>Published</deviceRecordStatus>
  <identifiers>
    <identifier>
      <deviceId>M930756120810</deviceId>
      <deviceIdType>Primary</deviceIdType>
      <deviceIdIssuingAgency>HIBCC</deviceIdIssuingAgency>
      <containsDINumber xsi:nil="true"></containsDINumber>
      <pkgQuantity xsi:nil="true"></pkgQuantity>
      <pkgDiscontinueDate xsi:nil="true"></pkgDiscontinueDate>
      <pkgStatus xsi:nil="true"></pkgStatus>
      <pkgType xsi:nil="true"></pkgType>
    </identifier>
  </identifiers>
  <brandName>Life Instruments</brandName>
  <gmdnTerms>
    <gmdn>
      <gmdnPTName>Orthopaedic knife</gmdnPTName>
      <gmdnPTDefinition>A hand-held manual surgical instrument designed for cutting/shaping bone during an orthopaedic surgical intervention. It is typically a heavy, one-piece instrument with a sharp, single-edged, strong cutting blade at the distal end available in various shapes and sizes, with a handle at the proximal end. It is normally made of high-grade stainless steel. This is a reusable device.</gmdnPTDefinition>
    </gmdn>
  </gmdnTerms>
  <productCodes>
    <fdaProductCode>
      <productCode>LXH</productCode>
      <productCodeName>Orthopedic Manual Surgical Instrument</productCodeName>
    </fdaProductCode>
  </productCodes>
  <deviceSizes/>
  <environmentalConditions/>
</device>
</gudid>

我正在使用下面的代码来解析这个 xml 文件。

from lxml import etree

file = "sample.xml"

root = etree.parse(file).xpath(
    "x:device", namespaces={"x": "http://www.fda.gov/cdrh/gudid"}
)

for event, element in etree.iterwalk(root, events=("start", "end")):
    if event == "start":
        print(event, etree.QName(element).localname, element.text)
    if event == "end":
        element.clear()

然而这一行

for event, element in etree.iterwalk(root, events=("start", "end")):

出现类似

的错误

TypeError:输入对象无效:列表

我看不出哪里错了?

.xpath(...) 方法 returns 一个元素列表,但您似乎假设它 returns 是单个元素。检查列表是否为空,然后使用此列表中的第一个元素:

devices = etree.parse(file).xpath(
    "x:device", namespaces={"x": "http://www.fda.gov/cdrh/gudid"}
)

if not devices:
    raise ValueError("No devices found")

device = devices[0]
for event, element in etree.iterwalk(device, events=("start", "end")):
    # rest of loop omitted...

另请注意,我已将变量名称从 root 更改为 device,因为它不是 XML 文档的根目录:<gudid>元素是.