Python元素树在标记XML中处理标记

2024-10-02 12:30:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理来自美国国立卫生研究院我在阅读与药物相关的禁忌症时遇到了一个问题。我想要标签里面的所有内容,但是一旦我点击了内部标签,它就会被切断。我尝试过遍历所有的子元素,但我能做的最好的就是显示“警告”“无尿;对……过敏”消失。如果有人知道使用解析器获取这些数据的方法,这将非常有帮助。 谢谢您!在

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

Tags: totexttitlelinkcodesection标签content
1条回答
网友
1楼 · 发布于 2024-10-02 12:30:51

假设您使用的是下面这样的东西,那么您需要使用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 "<<"

给予

^{pr2}$

相关问题 更多 >

    热门问题