0

我正在使用来自 dailymed.nlm.nih.gov 的 xml 标签。在阅读与药物相关的禁忌症时,我遇到了问题。我想要标签内的所有内容,但是一旦我点击内部标签,它就会中断。我试过迭代所有子元素,但我能做的最好的就是让“警告”显示出来。“;无尿;对……过敏”丢失了。如果有人知道使用解析器获取这些数据的方法,那将有很大帮助。谢谢!

 <component>
        <section ID="LINK_8e9e0719-efa5-451c-bea3-d547298ad0a1">
           <id root="8e9e0719-efa5-451c-bea3-d547298ad0a1"/>
           <code code="34070-3" codeSystem="2.16.840.1.113883.6.1" displayName="CONTRAINDICATIONS SECTION"/>
           <title>CONTRAINDICATIONS</title>
           <text>
              <paragraph>Atenolol and chlorthalidone tablets are contraindicated in patients with: sinus bradycardia; heart block greater than first degree; cardiogenic shock; overt cardiac failure (see<content styleCode="bold">
                    <linkHtml href="#LINK_0df2629f-13c7-4b14-8664-475c32377c68">WARNINGS</linkHtml>
                 </content>); anuria; hypersensitivity to this product or to sulfonamide-derived drugs.</paragraph>
           </text>
           <effectiveTime value="20101001"/>
        </section>
     </component>
4

1 回答 1

1

假设您正在使用类似以下内容的内容,您需要使用ET.tostring它来获取子元素的所有文本。

import xml.etree.ElementTree as ET
txt = """
<component>
<section ID="LINK_8e9e0719-efa5-451c-bea3-d547298ad0a1">
    <id root="8e9e0719-efa5-451c-bea3-d547298ad0a1"/>
    <code code="34070-3" codeSystem="2.16.840.1.113883.6.1" displayName="CONTRAINDICATIONS SECTION"/
    <title>CONTRAINDICATIONS</title>
    <text>
    <paragraph>Atenolol and chlorthalidone tablets are contraindicated in patients with: sinus brady
        <linkHtml href="#LINK_0df2629f-13c7-4b14-8664-475c32377c68">WARNINGS</linkHtml>
            </content>); anuria; hypersensitivity to this product or to sulfonamide-derived drugs.</
    </text>
    <effectiveTime value="20101001"/>
</section>
</component>"""

root = ET.fromstring(txt)

for e in root.iter('text'):
    print ">>"
    print ET.tostring(e, method="text")
    print "<<"

>>

    Atenolol and chlorthalidone tablets are contraindicated in patients with: sinus bradycardia; heart block greater than first degree; cardiogenic shock; overt cardiac failure (see
        WARNINGS
            ); anuria; hypersensitivity to this product or to sulfonamide-derived drugs.


<<
于 2013-02-10T20:37:04.110 回答