lxml 出现 TypeError: Invalid input object: list 错误

发布于 2025-01-14 03:22:43 字数 2769 浏览 1 评论 0原文

我正在尝试解析一个名为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: Invalid input object: list

我无法看到我错在哪里?

I am trying to parse a xml file with a name called sample.xml and the contents are as below.

<?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>

And i am using the below code to parse this xml file.

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()

However this line

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

errors out with an error like

TypeError: Invalid input object: list

I am unable to see where I am wrong here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑看君怀她人 2025-01-21 03:22:43

.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 文档的根: 元素是。

The .xpath(...) method returns a list of elements, but you seem to be assuming it returns a single element. Check that the list isn't empty and then use the first element out of this list:

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...

Note also that I've changed the name of the variable from root to device, as it's not the root of your XML document: the <gudid> element is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文