gpt4 book ai didi

python : get subelements from elements define in elementTree using a for loop

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:03 25 4
gpt4 key购买 nike

如果我有以下 xml:

<uima.cas.FSArray _id="7429" size="2">
<i>7409</i>
<i>7419</i>
</uima.cas.FSArray>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7342" codingScheme="SNOMEDCT" code="269435009" oid="269435009#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7322" codingScheme="SNOMEDCT" code="157754004" oid="157754004#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7352" codingScheme="SNOMEDCT" code="269432007" oid="269432007#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7332" codingScheme="SNOMEDCT" code="213029005" oid="213029005#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7362" codingScheme="SNOMEDCT" code="157762007" oid="157762007#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<uima.cas.FSArray _id="7372" size="5">
<i>7362</i>
<i>7332</i>
<i>7352</i>
<i>7322</i>
<i>7342</i>
</uima.cas.FSArray>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7235" codingScheme="SNOMEDCT" code="274241003" oid="274241003#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7265" codingScheme="SNOMEDCT" code="39384006" oid="39384006#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7255" codingScheme="SNOMEDCT" code="206825002" oid="206825002#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7275" codingScheme="SNOMEDCT" code="20262006" oid="20262006#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7245" codingScheme="SNOMEDCT" code="158202006" oid="158202006#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<uima.cas.FSArray _id="7285" size="5">
<i>7245</i>
<i>7275</i>
<i>7255</i>
<i>7265</i>
<i>7235</i>

我想要_id<i>节点 uima.cas.FSArray 的子元素

即查看第一个节点(前三行)我想检索类似的内容

_id    i
7429 7409
7429 7419

与以下类似 uima.cas.FSArray节点。

我意识到出现了相同的节点(没有属性),所以我只对带有 _id 的节点感兴趣元素。

这是我的尝试:

#!/usr/bin/env python

import sys
import os
import xml.etree.cElementTree as ET

tree = ET.ElementTree(file=sys.argv[-1])

UMLSarr = {}
for x in tree.iterfind('uima.cas.FSArray'):
UMLSarr[x] = x.attrib
subArr[x] = SubElement(UMLSarr[x],"subArr",attrib='i')

但我得到:

Traceback (most recent call last):
File "<stdin>", line 3, in <module>
NameError: name 'SubElement' is not defined

我已经尝试过此代码的各种其他迭代,但遇到了越来越多的错误,希望有人可以帮助我。

谢谢。

最佳答案

from lxml import etree

et = etree.fromstring(xml)
for array in et.xpath('//uima.cas.FSArray[@_id]'):
print(array.xpath('@_id'), array.xpath('./i/text()'))

输出:

['7429'] ['7409', '7419']
['7372'] ['7362', '7332', '7352', '7322', '7342']
['7285'] ['7245', '7275', '7255', '7265', '7235']

关于 python : get subelements from elements define in elementTree using a for loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41827654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com