首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XML到CSV问题

XML到CSV问题
EN

Stack Overflow用户
提问于 2018-04-10 20:41:32
回答 1查看 42关注 0票数 0

我有一个XML文件,其中包含文献检索的引用。我试图将其解析为CSV,以便使用excel打开,只导入一些节点。XML文件有几千个条目。其中一个条目是:

代码语言:javascript
复制
<records>
<rec resultID="1">
    <controlInfo>
      <bkinfo>
        <btl>Effect of an intervention based on basic Buddhist principles on the spiritual well-being of patients with terminal cancer.</btl>
      </bkinfo>
      <dissinfo />
      <jinfo>
        <jtl>European Journal of Oncology Nursing</jtl>
        <issn>14623889</issn>
      </jinfo>
      <pubinfo>
        <dt year="2017" month="12" day="01">Dec2017</dt>
        <vid>31</vid>
      </pubinfo>
      <artinfo>
        <ui type="doi">10.1016/j.ejon.2017.08.005</ui>
        <ppf>46</ppf>
        <ppct>6</ppct>
        <formats />
        <tig>
          <atl>Effect of an intervention based on basic Buddhist principles on the spiritual well-being of patients with terminal cancer.</atl>
        </tig>
        <aug>
          <au>Chimluang, Janya</au>
          <au>Thanasilp, Sureeporn</au>
          <affil>Faculty of Nursing, Chulalongkorn University, Bangkok, Thailand</affil>
        </aug>
        <ab>Purpose To evaluate the effect of an intervention based on basic Buddhist principles on the spiritual well-being of patients with terminal cancer. Methods This quasi-experimental research study had pre- and post-test control groups. The experimental group received conventional care and an intervention based on basic Buddhist principles for three consecutive days, including seven activities based on precept activities, concentration activities and wisdom activities. The control group received conventional care alone. Results Forty-eight patients participated in this study: 23 in the experimental group and 25 in the control group. Their mean age was 53 (standard deviation 10) years. The spiritual well-being of participants in the experimental group was significantly higher than that of participants in the control group at the second post-test ( P &lt; 0.05). Conclusions An intervention based on basic Buddhist principles improved the spiritual well-being of patients with terminal cancer. This result supports the beneficial effects of implementing this type of intervention for patients with terminal cancer.</ab>
        <pubtype>Academic Journal</pubtype>
        <doctype>research</doctype>
        <doctype>Article</doctype>
      </artinfo>
      <language>English</language>
    </controlInfo>
    <displayInfo>
      <pLink>
        <url>http://search.ebscohost.com/login.aspx?direct=true&amp;db=jlh&amp;AN=126392076&amp;site=ehost-live</url>
      </pLink>
    </displayInfo>
</rec>
</records>

我想做的是导入:

  • 第一个au (作者)
  • jtl (期刊标题)
  • dt (日期)
  • vid (卷号)
  • ppf (首页)
  • 页数
  • btl (文章标题)
  • ab (摘要)

我正在尝试以下代码:

代码语言:javascript
复制
import xml.etree.ElementTree as ET
import csv

tree = ET.parse("citations.xml")
root = tree.getroot()

# open a file for writing

citation_data = open('test.csv', 'w')

# create the csv writer object

csvwriter = csv.writer(citation_data)

count = 0

head = ['Author','Title']
csvwriter.writerow(head)

for member in root.findall('records'):
    citation = []
    au = member.find('rec').find('controlInfo').find('artinfo').find('aug').find('au').text
    citation.append(au)
    btl = member.find('rec').find('controlInfo').find('bkinfo').find('btl').text
    citation.append(btl)
    csvwriter.writerow(citation)
citation_data.close()

我得到了以下错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "test.py", line 22, in <module>
    au = member.find('controlInfo').find('artinfo').find('aug').find('au').text
AttributeError: 'NoneType' object has no attribute 'find'

我在这段代码中尝试了几个变体,包括没有反复出现的".find",但是我得到了同样的东西。

在这里的其他例子中,我找不到一个解决方案。我希望得到一些温和的指导和帮助,因为我对python是新手,这是我的第一个项目。

谢谢

一个

EN

回答 1

Stack Overflow用户

发布于 2018-04-10 20:49:18

xml中的某些条目<record>缺少此结构。

代码语言:javascript
复制
<artinfo>
  <aug>

所以member.find('controlInfo').find('artinfo').find('aug').find('au')找不到其中一个标签并返回None,所以您缺少了<aug><artinfo>

检查每个查找后的返回值不是None,以便调用下一个find,smth如下

代码语言:javascript
复制
a = member.find('controlInfo')
if a is not None: 
  a = a.find('artinfo') 

and so on...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49762550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档