1

我有一个 .xml 文件的文件夹,如下所示:

<PubmedArticleSet>
  <PubmedArticle>
    <MedlineCitation Owner="NLM" Status="MEDLINE">
      <PMID Version="1">23458631</PMID>
      <DateCreated>
        <Year>2013</Year>
        <Month>04</Month>
        <Day>08</Day>
      </DateCreated>
      <MeshHeadingList>
        <MeshHeading>
          <DescriptorName MajorTopicYN="N">Animals</DescriptorName>
        </MeshHeading>
        <MeshHeading>
          <DescriptorName MajorTopicYN="N">Calcium</DescriptorName>
          <QualifierName MajorTopicYN="Y">metabolism</QualifierName>
        </MeshHeading>
        <MeshHeading>
          <DescriptorName MajorTopicYN="N">Calcium Chloride</DescriptorName>
          <QualifierName MajorTopicYN="N">administration &amp; dosage</QualifierName>
        </MeshHeading>
      </MeshHeadingList>
    </MedlineCitation>
  </PubmedArticle>
  <PubmedArticle>
    <MedlineCitation Status="Publisher" Owner="NLM">
      <PMID Version="1">23458629</PMID>
      <DateCreated>
        <Year>2013</Year>
        <Month>3</Month>
        <Day>20</Day>
      </DateCreated>
      <MeshHeadingList>
        <MeshHeading>
          <DescriptorName MajorTopicYN="N">Adolescent</DescriptorName>
        </MeshHeading>
        <MeshHeading>
          <DescriptorName MajorTopicYN="N">Adult</DescriptorName>
        </MeshHeading>
        <MeshHeading>
          <DescriptorName MajorTopicYN="N">Anthropometry</DescriptorName>
        </MeshHeading>
      </MeshHeadingList>
    </MedlineCitation>
  </PubmedArticle>
</PubmedArticleSet>

我想使用 Python 解析 XML 文件并为每篇文章提取 PMID、DateCreated、所有 DescriptorName 和 MajorTopicYN。然后,将结果另存为 .txt 文件,如下所示:

ArticleID|CreatedDate|MeSH|IsMajor
23458631|20130408|Animals|N
23458631|20130408|Calcium|N
23458631|20130408|Calcium Chloride|N
23458629|20130320|Adolescent|N
23458629|20130320|Adult|N
23458629|20130320|Anthropometry|N
4

3 回答 3

4

干得好。

import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()
with open('my_text_file.txt', 'w') as f:
    f.write('ArticleID|CreatedDate|MeSH|IsMajor\n')
for pubmed_article in root.findall('PubmedArticle'):
    ArticleID = pubmed_article.find('MedlineCitation').find('PMID').text
    year = pubmed_article.find('MedlineCitation').find('DateCreated').find('Year').text
    month = pubmed_article.find('MedlineCitation').find('DateCreated').find('Month').text
    day = pubmed_article.find('MedlineCitation').find('DateCreated').find('Day').text
    CreatedDate = year + month + day
    for mesh_heading in pubmed_article.find('MedlineCitation').find('MeshHeadingList').findall('MeshHeading'):
        MeSH = mesh_heading.find('DescriptorName').text
        IsMajor = mesh_heading.find('DescriptorName').get('MajorTopicYN')
        line_to_write = ArticleID + '|' + CreatedDate + '|' + MeSH + '|' + IsMajor + '\n'
        with open('my_text_file.txt', 'a') as f:
            f.write(line_to_write)

这是输出文件

ArticleID|CreatedDate|MeSH|IsMajor
23458631|20130408|Animals|N
23458631|20130408|Calcium|N
23458631|20130408|Calcium Chloride|N
23458629|20130320|Adolescent|N
23458629|20130320|Adult|N
23458629|20130320|Anthropometry|N
于 2013-11-04T17:21:22.640 回答
1

使用 ElementTree http://docs.python.org/2/library/xml.etree.elementtree.html

import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()
for pmid in root.iter('PMID'):
    print pmid.text

这个的输出是

23458631
23458629

获得元素值后,您可以构建字符串并将它们写入文件。

于 2013-11-04T16:57:14.947 回答
0

这是我的版本:

import xml.etree.ElementTree as ET

xml_path = r'Y:\Misc\stack_overflow\Python\xml_extract\data.xml'
output_file_path = 'output.txt'
f = open(output_file_path, 'wb')
f.write('ArticleID|CreatedDate|MeSH|IsMajor\n')

tree = ET.parse(xml_path)
root = tree.getroot()

for pa in root.iter('PubmedArticle'):
    ArticleID = pa.find('MedlineCitation/PMID').text
    CreatedDate = pa.find('MedlineCitation/DateCreated/Year').text+\
                  pa.find('MedlineCitation/DateCreated/Month').text.zfill(2)+\
                  pa.find('MedlineCitation/DateCreated/Day').text.zfill(2)
    for mh in pa.iter('MeshHeading'):
        DescriptorName = mh.find('DescriptorName').text
        MajorTopicYN = mh.find('DescriptorName').attrib['MajorTopicYN']
        f.write(ArticleID+'|'+CreatedDate+'|'+DescriptorName+'|'+MajorTopicYN+'\n')
f.close()

文件中的输出是:

ArticleID|CreatedDate|MeSH|IsMajor
23458631|20130408|Animals|N
23458631|20130408|Calcium|N
23458631|20130408|Calcium Chloride|N
23458629|20130320|Adolescent|N
23458629|20130320|Adult|N
23458629|20130320|Anthropometry|N
于 2013-11-04T17:41:18.147 回答