xml解析此特定xml

时间:2015-10-19 14:19:41

标签: python xml

    <instance id="activate.v.bnc.00024693" docsrc="BNC">
<answer instance="activate.v.bnc.00024693" senseid="38201"/>
<context>
Do you know what it is ,  and where I can get one ?  We suspect you had seen the Terrex Autospade ,  which is made by Wolf Tools .  It is quite a hefty spade , with bicycle - type handlebars and a sprung lever at the rear , which you step on to <head>activate</head> it . Used correctly ,  you should n't have to bend your back during general digging ,  although it wo n't lift out the soil and put in a barrow if you need to move it !  If gardening tends to give you backache ,  remember to take plenty of rest periods during the day ,  and never try to lift more than you can easily cope with .  
</context>
</instance>

我想提取所有内容。这就是我现在拥有的。 stuff.text只在<head></head>之前打印文本(即你知道......继续),但我不知道如何在</head>之后提取后半部分(即它使用......轻松应对。)

import xml.etree.ElementTree as et
tree = et.parse(os.getcwd()+"/../data/train.xml")
instance = tree.getroot()

    for stuff in instance:
        if(stuff.tag == "answer"):
            print "the correct answer is %s" % stuff.get('senseid')
        if(stuff.tag == "context"):
            print dir(stuff)
            print stuff.text

2 个答案:

答案 0 :(得分:0)

如果使用BeautifulSoup是一个选项,那将是微不足道的:

import bs4
xtxt = '''        <instance id="activate.v.bnc.00024693" docsrc="BNC">
    <answer instance="activate.v.bnc.00024693" senseid="38201"/>
    <context>
    Do you know what it is ,  and where I can get one ?  We suspect you had seen the Terrex Autospade ,  which is made by Wolf Tools .  It is quite a hefty spade , with bicycle - type handlebars and a sprung lever at the rear , which you step on to <head>activate</head> it . Used correctly ,  you should n't have to bend your back during general digging ,  although it wo n't lift out the soil and put in a barrow if you need to move it !  If gardening tends to give you backache ,  remember to take plenty of rest periods during the day ,  and never try to lift more than you can easily cope with .  
    </context>
    </instance>'''
soup = bs4.BeautifulSoup(xtxt)
print soup.find('context').text

给出:

Do you know what it is ,  and where I can get one ?  We suspect you had
seen the Terrex Autospade ,  which is made by Wolf Tools .  It is quite 
a hefty spade , with bicycle - type handlebars and a sprung lever at the 
rear , which you step on to activate it . Used correctly ,  you shouldn't 
have to bend your back during general digging ,  although it wo n't lift 
out the soil and put in a barrow if you need to move it !  If gardening 
tends to give you backache ,  remember to take plenty of rest periods 
during the day ,  and never try to lift more than you can easily cope 
with .  

如果您喜欢使用ElementTree,则应使用itertext处理所有文本:

import xml.etree.ElementTree as et
tree = et.parse(os.getcwd()+"/../data/train.xml")
instance = tree.getroot()

    for stuff in instance:
        if(stuff.tag == "answer"):
            print "the correct answer is %s" % stuff.get('senseid')
        if(stuff.tag == "context"):
            print dir(stuff)
            print ''.join(stuff.itertext())

如果您确定您的xml文件是正确的,那么ElementTree就足够了,因为它是标准Python库的一部分,您将没有外部依赖性。但是如果XML形成错误,BeautifulSoup可以很好地修复小错误。

答案 1 :(得分:0)

可以使用元素序列化。有两种选择:

  • 保持内部<head></head>
  • 只返回没有任何标签的文字。

如果使用代码进行序列化,则可以手动删除外部<context></context>标记:

# convert element to string and remove <context></context> tag
print(et.tostring(stuff).strip().lstrip('<context>').rstrip('</context>')))
# read only text without any tags
print(et.tostring(stuff, method='text'))