Python-XML:检查某个标签是否不存在然后继续?

时间:2016-04-01 11:14:28

标签: python xml element

我有一个XML文件,我使用的是元素树。

例如我有这个XML python:

<TEXT>

<PHRASE>
<en x='LOC'>NY</en>
<PREP>is</PREP>
<PREP>not</PREP>
<en x='LOC'>Mexico</en>
</PHRASE>

<PHRASE>
<en x='LOC'>NY</en>
<PREP>is</PREP>
<PREP>in</PREP>
<en x='LOC'>USA</en>
</PHRASE>

<PHRASE>
<en x='ORG'>Alpha</en>
<CONJ>is</CONJ>
<NEG>not</NEG>
<PREP>in</PREP>
<en x='LOC'>Atlanta</en> 
</PHRASE> 

<PHRASE>
<en x='ORG'>Google</en>
<CONJ>is</CONJ>
<PREP>in</PREP>
<en x='LOC'>California</en> 
</PHRASE> 


</TEXT>

如果他们没有<NEG>标签,我想提取对: 我希望输出为:

NY-USA

Google California

我试过了:

neg= elt.findall('NEG')
                if neg is None:
                 continue

但它没有工作

import xml.etree.ElementTree as ET
tree = ET.parse('TrainBaseEnglish.xml')
root = tree.getroot()
print("------------------------ORG-LOC-------------------------------")
ORG_LOCcount=0
for phrase in root.findall('./PHRASE'):
    ens = {en.get('x'): en.text for en in phrase.findall('en')}
    if 'ORG' in ens and 'LOC' in ens:
        print("ORG is: {}, LOC is: {} /".format(ens["ORG"], ens["LOC"]))
        #print(ens["ORG"])
        #print(ens["PERS"])
        ORG_LOCcount = ORG_LOCcount + 1
print("Number of ORG_LOC relation", ORG_LOCcount)
print("------------------------LOC-LOC-------------------------------")
LOC_LOCcount=0
for phrase in root:
    if phrase.tag == 'PHRASE':
        collected_names = []
        for elt in phrase:
             if elt.tag == 'en':
                if 'x' in elt.attrib and elt.attrib['x'] == 'LOC':
                    collected_names += [elt.text]
        if len(collected_names) >= 2:
            print("LOC is: {}, LOC is: {} /".format(collected_names[0],collected_names[1]))
            LOC_LOCcount = LOC_LOCcount + 1
print("Number of LOC_LOC relation", LOC_LOCcount)

1 个答案:

答案 0 :(得分:0)

如果每NEG只有一个PHRASE元素,则可以执行此操作

import xml.etree.ElementTree as ET

xml_string = '''<TEXT>

<PHRASE>
<en x='LOC'>NY</en>
<PREP>is</PREP>
<PREP>not</PREP>
<en x='LOC'>Mexico</en>
</PHRASE>

<PHRASE>
<en x='LOC'>NY</en>
<PREP>is</PREP>
<PREP>in</PREP>
<en x='LOC'>USA</en>
</PHRASE>

<PHRASE>
<en x='ORG'>Alpha</en>
<CONJ>is</CONJ>
<NEG>not</NEG>
<PREP>in</PREP>
<en x='LOC'>Atlanta</en> 
</PHRASE> 

<PHRASE>
<en x='ORG'>Google</en>
<CONJ>is</CONJ>
<PREP>in</PREP>
<en x='LOC'>California</en> 
</PHRASE> 


</TEXT>
'''

xml = ET.fromstring(xml_string)
phrases = xml.findall("PHRASE")

for phrase in phrases:
    neg = phrase.find("NEG")
    if neg is None:
        continue
    print neg

如果您预计每个NEG有多个PHRASE个元素,并且需要使用.findall,那么您应该检查自neg以来.findall的长度}返回一个列表。

for phrase in phrases:
    neg = phrase.findall("NEG")
    if len(neg) == 0:
        continue
    print neg