代码之家  ›  专栏  ›  技术社区  ›  jhon.smith

lxml错误输出为TypeError:无效输入对象:列表

  •  0
  • jhon.smith  · 技术社区  · 2 年前

    我试图解析一个名为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:无效的输入对象:列表

    我看不出我错在哪里?

    1 回复  |  直到 2 年前
        1
  •  1
  •   Luke Woodward    2 年前

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

    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> 元素是。