lxml 出现 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: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.xpath(...)
方法返回一个元素列表,但您似乎假设它返回单个元素。检查列表是否为空,然后使用此列表中的第一个元素:另请注意,我已将变量名称从
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:Note also that I've changed the name of the variable from
root
todevice
, as it's not the root of your XML document: the<gudid>
element is.