在python中删除xml解析中的外部参照标记

时间:2013-10-11 14:24:18

标签: python xml elementtree

我正在尝试使用ElementTree在python中解析一些像这样结构化的.nxml文件.....

<body>
    <sec>
        <title>INTRODUCTION</title>
        <p>Experimentation with substances usually takes place during adolescence [<xref ref-type="bibr" rid="b1">1</xref>]. Adolescents are highly vulnerable to social influences [<xref ref-type="bibr" rid="b2">2</xref>], have lower tolerance levels and become dependent at lower doses than adults [<xref ref-type="bibr" rid="b3">3</xref>]. Adolescent-onset substance abuse is characterized by more rapid development of multiple drug dependencies and more severe psychopathology [<xref ref-type="bibr" rid="b4">4</xref>]. However, the majority of adolescents who experiment with substances do not become problem users. A better understanding is needed of the factors underlying initiation of substance use in adolescence versus heavy use and problem use. Specifically, if the liability to progress to heavier substance use is influenced by processes other than those that influence initiation, then primary prevention/intervention programmes can be only partly effective. It may be more successful, in terms of both cost and impact, to target those factors implicated in the progression to heavy/problem use. However, if the underlying liabilities to initiation and progression were strongly related, interventions could be tailored to both behaviours.</p>

具体来说,我试图在

之间提取文本
<p> </p> tags. 

然而元素

[<xref> </xref>] 
文中的

正在打断解析。

我尝试过使用

for sec in body:
    for p in sec:
        for e in p:
           e.remove (xref)

但元素无法识别。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

这更有可能奏效:

for xref in body.findall('xref'):
    body.remove(xref)

为了更符合您的目标,请尝试:

for sec in body.findall('sec'):
    for p in sec.findall('p'):
        for e in p.findall('xref'):
           p.remove(e)

答案 1 :(得分:0)

实际上我把它全部废弃并使用BeautifulSoup删除所有标签。做了一个享受。不能相信我真是个笨蛋。