从xml文档获取文本

时间:2019-03-13 11:05:55

标签: python xml xpath

我想获取PMID,对于每个PMID,可以从作者列表中获取其他列表,对于每个PMID,我可以获取作者列表,对于所有其他PMId,我可以获取作者列表

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE PubmedArticleSet SYSTEM "http://dtd.nlm.nih.gov/ncbi/pubmed/out/pubmed_190101.dtd">
<PubmedArticleSet>
 <PubmedArticle>
<MedlineCitation Status="MEDLINE" Owner="NLM">
  <PMID Version="1">2844048</PMID>
  <DateCompleted>
    <Year>1988</Year>
    <Month>10</Month>
    <Day>26</Day>
  </DateCompleted>
  <DateRevised>
    <Year>2010</Year>
    <Month>11</Month>
    <Day>18</Day>
  </DateRevised>
    <AuthorList CompleteYN="Y">
      <Author ValidYN="Y">
        <LastName>Guarner</LastName>
        <ForeName>J</ForeName>
        <Initials>J</Initials>
        <AffiliationInfo>
          <Affiliation>Department of Pathology and Laboratory Medicine, Emory University Hospital, Atlanta, Georgia.</Affiliation>
        </AffiliationInfo>
      </Author>
      <Author ValidYN="Y">
        <LastName>Cohen</LastName>
        <ForeName>C</ForeName>
        <Initials>C</Initials>
      </Author>
</AuthorList>
</MedlineCitation>

由于标签的结构,我可以单独获取,但不知道如何分组。

tree = ET.parse('x.xml')
root = tree.getroot()

pid =[]
for pmid in root.iter('PMID'):
   pid.append(pmid.text)

lastname=[]
for id in root.findall("./PubmedArticle/MedlineCitation/Article/AuthorList"):
for ln in id.findall("./Author/LastName"):
    lastname.append(ln.text)

forename=[]
for id in root.findall("./PubmedArticle/MedlineCitation/Article/AuthorList"):
for fn in id.findall("./Author/ForeName"):
    forename.append(fn.text)

initialname=[]
for id in root.findall("./PubmedArticle/MedlineCitation/Article/AuthorList"):
for i in id.findall("./Author/Initials"):
   initialname.append(i.text)

预期产量

PMID               AUTHORS
2844048            'Guarner J J', 'Cohen C C'

请提出解决问题的可能方法,预期输出的行数将更多,谢谢。

2 个答案:

答案 0 :(得分:0)

XPath 1.0的数据模型在specification中定义:

  

3.3节点集

     

3.4布尔

     

3.5个数字

     

3.6字符串

节点集是适当的集:重复数据删除和无序。您需要一个sequence,即数据的有序列表(例如,节点集的有序列表)。此数据类型是XPath 2.0及更高版本的一部分。

要在XPath 1.0中将其分组为嵌入式语言,请选择“同类中的第一”,然后使用宿主语言遍历文档以获取分组的项目,即使使用其他XPath表达式也是如此。这就是在XSLT本身中完成的方式。

答案 1 :(得分:0)

虽然花了一段时间,但我想我明白了。为了使此练习有趣,我进行了一些更改。

首先,您问题中的xml代码无效; you can check it here, for example

因此,首先我修复了xml。另外,我将其转换为PubmedArticleSet,因此它有2篇文章,第一篇文章有​​3位作者,第二篇文章-两位(显然是虚假信息),目的是确保代码能够全部抓住它们。为了简化起见,我删除了一些与本练习无关的信息,例如隶属关系。

这就是离开我们的地方。 首先,修改xml:

source = """
<PubmedArticleSet>
<PubmedArticle>
    <MedlineCitation Status="MEDLINE" Owner="NLM">
        <PMID Version="1">2844048</PMID>
        <AuthorList CompleteYN="Y">
            <Author ValidYN="Y">
                <LastName>Guarner</LastName>
                <ForeName>J</ForeName>
                <Initials>J</Initials>
            </Author>
            <Author ValidYN="Y">
                <LastName>Cohen</LastName>
                <ForeName>C</ForeName>
                <Initials>C</Initials>
            </Author>
            <Author ValidYN="Y">
                <LastName>Mushi</LastName>
                <ForeName>E</ForeName>
                <Initials>F</Initials>
            </Author>
        </AuthorList>
    </MedlineCitation>
</PubmedArticle>
<PubmedArticle>
    <MedlineCitation Status="MEDLINE" Owner="NLM">
        <PMID Version="1">123456</PMID>
        <AuthorList CompleteYN="Y">
            <Author ValidYN="Y">
                <LastName>Smith</LastName>
                <ForeName>C</ForeName>
                <Initials>C</Initials>
            </Author>
            <Author ValidYN="Y">
                <LastName>Jones</LastName>
                <ForeName>E</ForeName>
                <Initials>F</Initials>
            </Author>
        </AuthorList>
    </MedlineCitation>
</PubmedArticle>

 """

接下来,导入需要导入的内容:

from lxml import etree
import pandas as pd

接下来,代码:

doc = etree.fromstring(source)
art_loc = '..//*/PubmedArticle' #this is the path to all the articles
#count the number of articles in the article set - that number is a float has to be converted to integer before use:
num_arts = int(doc.xpath(f'count({art_loc})')) # or could use len(doc.xpath(f'({art_loc})')) 
grand_inf = [] #this list will hold the accumulated information at the end
for art in range(1,num_arts+1): #can't do range(num_arts) because of the different ways python and Pubmed count
    loc_path = (f'{art_loc}[{art}]/*/') #locate the path to each article
    #grab the article id:
    id_path = loc_path+'PMID'
    pmid = doc.xpath(id_path)[0].text
    art_inf = [] #this list holds the information for each article
    art_inf.append(pmid)
    art_path = loc_path+'/Author' #locate the path to the author group
    #determine the number of authors for this article; again, it's a float which needs to converted to integer
    num_auths = int(doc.xpath(f'count({art_path})')) #again: could use len(doc.xpath(f'({art_path})'))

    auth_inf = [] #this will hold the full name of each of the authors

    for auth in range(1,num_auths+1):
        auth_path = (f'{art_path}[{auth}]') #locate the path to each author
        LastName = doc.xpath((f'{auth_path}/LastName'))[0].text
        FirstName = doc.xpath((f'{auth_path}/ForeName'))[0].text
        Middle = doc.xpath((f'{auth_path}/Initials'))[0].text
        full_name = LastName+' '+FirstName+' '+Middle
        auth_inf.append(full_name)
   art_inf.append(auth_inf)
   grand_inf.append(art_inf)

最后,将此信息加载到数据框中:

df=pd.DataFrame(grand_inf,columns=['PMID','Author(s)'])
df

输出:

     PMID       Author(s)
 0   2844048    [Guarner J J, Cohen C C, Mushi E F]
 1   123456     [Smith C C, Jones E F]

我们现在可以休息...