2013-10-11 2 views
0

Я пытаюсь разобрать в Python с использованием ElementTree некоторые .nxml файлы структурированы, как это .....удаление XREF теги в XML-синтаксического анализа в питона

<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) 

но элементы не распознаются. Есть идеи?

+0

Что значит «не признано»? – aIKid

ответ

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) 
0

На самом деле я слом все это и используется BeautifulSoup просто удалить все теги. Работал. Не могу поверить, что я был таким шманом.

+0

Почему бы нам не показать, как вы на самом деле это сделали? В настоящее время это не очень большой ответ. – mzjn

Смежные вопросы