我有一个XML文件,其中包含文献检索的引用。我试图将其解析为CSV,以便使用excel打开,只导入一些节点。XML文件有几千个条目。其中一个条目是:
<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 < 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&db=jlh&AN=126392076&site=ehost-live</url>
</pLink>
</displayInfo>
</rec>
</records>我想做的是导入:
我正在尝试以下代码:
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()我得到了以下错误:
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是新手,这是我的第一个项目。
谢谢
一个
发布于 2018-04-10 20:49:18
xml中的某些条目<record>缺少此结构。
<artinfo>
<aug>所以member.find('controlInfo').find('artinfo').find('aug').find('au')找不到其中一个标签并返回None,所以您缺少了<aug>或<artinfo>
检查每个查找后的返回值不是None,以便调用下一个find,smth如下
a = member.find('controlInfo')
if a is not None:
a = a.find('artinfo')
and so on...https://stackoverflow.com/questions/49762550
复制相似问题