繁体   English   中英

标签XML中的Python元素树处理标签

[英]Python Element Tree Handling Tags within Tags XML

我正在使用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>

假设您正在使用类似以下内容的内容,则需要使用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.


<<

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM