【问题标题】:Multiple XML file paring into a single CSV多个 XML 文件配对成单个 CSV
【发布时间】:2019-07-26 23:07:07
【问题描述】:

我在一个文件夹中存在多个 XML 文件,需要检索几个标签的信息 在 Excel 中想要检索 AbstractText Label="FINDINGS"AbstractText Label="IMPRESSION"parentImage id 标签的详细信息,并将此信息从所有 XML 文件存储到一个 csv。

我想从给定的示例 XML 文件中检索 AbstractText Label="FINDINGS"AbstractText Label="IMPRESSION"parentImage id 的详细信息,并将所有 XML 文件中的这些信息存储在 Excel 工作表中。

编辑:我想知道如何获取单个文件夹中存在的所有.xml 文件的这些详细信息,并将这些信息作为列和它们各自的值从这些 xml 标记写入单个 csv。

<?xml version="1.0" encoding="utf-8"?>
<eCitation>
    <meta type="rr"/>
    <uId id="CXR49"/>
    <pmcId id="49"/>
    <docSource>CXR</docSource>
    <IUXRId id="49"/>
    <licenseType>open-access</licenseType>
    <licenseURL>http://creativecommons.org/licenses/by-nc-nd/4.0/</licenseURL>
    <ccLicense>byncnd</ccLicense>
    <articleURL/>
    <articleDate>2013-08-01</articleDate>
    <articleType>XR</articleType>
    <publisher>Indiana University</publisher>
    <title>Indiana University Chest X-ray Collection</title>
    <note>The data are drawn from multiple hospital systems.</note>
    <specialty>pulmonary diseases</specialty>
    <subset>CXR</subset>
    <MedlineCitation Owner="Indiana University" Status="supplied by publisher">
        <Article PubModel="Electronic">
            <Journal>
                <JournalIssue>
                    <PubDate>
                        <Year>2013</Year>
                        <Month>08</Month>
                        <Day>01</Day>
                    </PubDate>
                </JournalIssue>
            </Journal>
            <ArticleTitle>Indiana University Chest X-ray Collection
</ArticleTitle>
            <Abstract>
                <AbstractText Label="COMPARISON">None.
</AbstractText>
                <AbstractText Label="INDICATION">XXXX-year-old with
osteoarthritis of the hip scheduled for total hip replacement.
Preoperative evaluation.
</AbstractText>
                <AbstractText Label="FINDINGS">The heart, pulmonary XXXX and
mediastinum are within normal limits. There is no pleural
effusion or pneumothorax. There is no focal air space opacity to
suggest a pneumonia. There are degenerative changes of the
thoracic spine. There is a calcified granuloma identified in the
right suprahilar region. The aorta is mildly tortuous and
ectatic. There is asymmetric right apical smooth pleural
thickening. There are severe degenerative changes of the XXXX.
</AbstractText>
                <AbstractText Label="IMPRESSION">No acute cardiopulmonary
disease.
</AbstractText>
            </Abstract>
            <Affiliation>Indiana University</Affiliation>
            <AuthorList CompleteYN="Y">
                <Author ValidYN="Y">
                    <LastName>Kohli</LastName>
                    <ForeName>Marc</ForeName>
                    <Initials>MD</Initials>
                </Author>
                <Author ValidYN="Y">
                    <LastName>Rosenman</LastName>
                    <ForeName>Marc</ForeName>
                    <Initials>M</Initials>
                </Author>
            </AuthorList>
            <Language>eng</Language>
            <PublicationTypeList>
                <PublicationType>Radiology Report</PublicationType>
            </PublicationTypeList>
            <ArticleDate>
                <Year>2013</Year>
                <Month>08</Month>
                <Day>01</Day>
            </ArticleDate>
        </Article>
        <EssieArticleTitle>Indiana University Chest X-ray                 Collection</EssieArticleTitle>
        <IMedAuthor>Marc David Kohli MD</IMedAuthor>
        <IMedAuthor>Marc Rosenman M</IMedAuthor>
    </MedlineCitation>
    <MeSH>
        <major>Thoracic Vertebrae/degenerative</major>
        <major>Calcified Granuloma/lung/hilum/right</major>
        <major>Aorta/tortuous/mild</major>
        <major>Thickening/pleura/apex/right</major>
        <automatic>calcified granuloma</automatic>
        <automatic>degenerative change</automatic>
        <automatic>pleural thickening</automatic>
    </MeSH>
    <parentImage id="CXR49_IM-2110-1001">
        <figureId>F1</figureId>
        <caption>PA and lateral chest radiographs dated XXXX at XXXX hours.
</caption>
        <panel type="single">
            <url>/hadoop/storage/radiology/extract/CXR49_IM-2110-1001.jpg</url>
            <imgModality>7</imgModality>
            <region type="panel">
                <globalImageFeatures>
                    <CEDD>f2p0k1205</CEDD>
                    <ColorLayout>f1p0k137</ColorLayout>
                    <EdgeHistogram>f0p0k184</EdgeHistogram>
                    <FCTH>f4p0k2450</FCTH>
                    <SemanticContext60>f3p0k74</SemanticContext60>
                </globalImageFeatures>
            </region>
        </panel>
    </parentImage>
    <parentImage id="CXR49_IM-2110-2001">
        <figureId>F2</figureId>
        <caption>PA and lateral chest radiographs dated XXXX at XXXX hours.            </caption>
        <panel type="single">
            <url>/hadoop/storage/radiology/extract/CXR49_IM-2110-2001.jpg</url>
            <imgModality>7</imgModality>
            <region type="panel">
                <globalImageFeatures>
                    <CEDD>f2p0k710</CEDD>
                    <ColorLayout>f1p0k83</ColorLayout>
                    <EdgeHistogram>f0p0k1200</EdgeHistogram>
                    <FCTH>f4p0k369</FCTH>
                    <SemanticContext60>f3p0k18</SemanticContext60>
                </globalImageFeatures>
            </region>
        </panel>
    </parentImage>
</eCitation>

【问题讨论】:

  • 请清楚地提供可重现的代码和预期的输出。
  • 嗨桑托什,感谢您的回复。我希望上面指定的每个 xml 标记(父图像,AbstractText label="Finding",AbstractText label="Impression")作为 csv 中的列标题,它们各自的行值应该是包含在这些属性中的数据。有 4000 个 xml 文件,每个文件都有相似的格式。所有这些文件都有一个唯一的图像 ID 以及与它们相关的各自的发现。我需要完整的随附数据。我怎样才能做到这一点。我对 XML 完全陌生。
  • 让我们从一个文件开始,好吗?如果一个函数可以解析一个文件并返回每个文件的字典列表或某种格式,是否可以?
  • 是的,桑托什,只要我收到所需的数据,我就完全没问题。我需要一个包含所有 4000 个 xml 文件信息的单个 csv。

标签: python r xml xml-parsing


【解决方案1】:

假设您的 xml 存在于 xmldoc.txt 中,以下脚本将以列表的形式获取您所需的值。您可以修改代码以满足您的需要。

from lxml import etree
import pandas as pd
df = pd.DataFrame(columns=['X', 'Y', 'Z', 'W'])#change it to what you want
for f in xmlfiles: # xmlfiles is a list of strings where each one is the absolute path to your xml files
    tree = etree.parse(f)
    findings = tree.find(".//AbstractText[@Label='FINDINGS']")
    impression = tree.find(".//AbstractText[@Label='IMPRESSION']")
    parentimages = tree.findall(".//parentImage")
    x = [findings,impression]
    y = [i.text for i in x]
    z = [i.attrib['id'] for i in parentimages]
    y.extend(z)
    df.loc[len(df)] = y
df.to_csv('everything.csv',header=TRUE)

【讨论】:

  • 只有 1 个问题,我有 .xml 文件,它也会运行吗?
  • 文件的扩展名无关紧要,只要它是一个文本文件。当然,xml 是一种文本文件。快乐编码。 :)
  • 是的,这非常有效......非常感谢。但我想知道如何获取单个文件夹中存在的所有 .xml 文件的这些详细信息?其次,如何将这些信息作为列和它们各自的值写入 csv?
  • 您需要更新上述问题中的那些问题,可能是子查询,标记为“EDIT:”,供我们任何人回答。
  • 嘿...我尝试实现上述编辑,但出现以下错误:OSError: Error reading file 'C': failed to load external entity "C"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-15
  • 2021-08-17
  • 1970-01-01
相关资源
最近更新 更多