用if else写嵌套的for循环的csv

时间:2019-01-07 20:35:48

标签: python xml csv

我是python的新手,我每天都学到很多东西。我有一个特定的文件夹,其中包含一些xml文件,我正在解析PMID,Date,Title和Abstract的xml文本,并且我正在使用if else语句编写for循环的csv,但是它没有打印出来,因此给出错误,如何编写for的csv循环使用if else条件

这是我的python代码:

AnimatorController

错误:

import os 
try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
import csv

path = '/home/shayez/Desktop/project/kk'

listfile = []
files = os.listdir(path)
for name in files:

    listfile.append(name)

pmdata = []    

for name2 in listfile:

        full_file = os.path.abspath(os.path.join('project/kk',name2))
        dom = ET.parse(full_file)
        pmdat = dom.findall('PubmedArticle')
        pmdata.append(pmdat)                

def Print_Data ():

    header = ['PMID','Date','Title','Abstract']
    with open ('/home/shayez/Desktop/karim.csv','wt') as csvfile:
        writer = csv.writer(csvfile, delimiter ="\t" )
        writer.writerow(header)

    for d in pmdata:

                 for c in d :

                     PMID = c.find('MedlineCitation/PMID').text
                     title = c.find('MedlineCitation/Article/ArticleTitle').text
                     Date = c.find('MedlineCitation/Article/Journal/JournalIssue/PubDate/Year')
                     Date2 = c.find('MedlineCitation/Article/Journal/JournalIssue/PubDate/MedlineDate')

                     Abstract = c.find('MedlineCitation/Article/Abstract/AbstractText')

                     rows =  [PMID,Date,title,Abstract]

                     if Abstract is None :
                         print PMID,"\t",Date.text, "\t",title ,"\t", "No abstract Available"

                     elif Date2 is None:
                         print PMID,"\t",Date.text, "\t",title ,"\t",Abstract.text   

                     elif Date is None:
                         print PMID,"\t",Date2.text, "\t",title ,"\t",Abstract.text

                     else :
                         print PMID,"\t","No Date", "\t",title ,"\t", "No abstract Available"

                     rows =  [PMID,Date,title,Abstract]

                     writer.writerows(rows)

Print_Data()

1 个答案:

答案 0 :(得分:0)

您需要将for循环放置在with块内 ,否则它将关闭csvfile

def Print_Data ():

    header = ['PMID','Date','Title','Abstract']
    with open ('/home/shayez/Desktop/karim.csv','wt') as csvfile:
        writer = csv.writer(csvfile, delimiter ="\t" )
        writer.writerow(header)

        for d in pmdata:

             for c in d :

                     PMID = c.find('MedlineCitation/PMID').text
                     title = c.find('MedlineCitation/Article/ArticleTitle').text
                     Date = c.find('MedlineCitation/Article/Journal/JournalIssue/PubDate/Year')
                     Date2 = c.find('MedlineCitation/Article/Journal/JournalIssue/PubDate/MedlineDate')

                     Abstract = c.find('MedlineCitation/Article/Abstract/AbstractText')

                     rows =  [PMID,Date,title,Abstract]

                     # Other code

                     writer.writerows(rows)

现在您已经将writer移到了with块中,我们可以解决另一个问题。 writer.writerows()期望有row个对象的可迭代。您的rows对象是单个行,引发异常。要完成您想做的事情,您必须将所有要写入row的{​​{1}}条目收集起来:

list

您还有另一个选择是在每个with open ('/home/shayez/Desktop/karim.csv','wt') as csvfile: writer = csv.writer(csvfile, delimiter ="\t" ) writer.writerow(header) rows = [] for d in pmdata: for c in d: # code to get PMID, Date, title, Abstract rows.append([PMID, Date, title, Abstract]) # Note that this is after your two for loops writer.writerows(rows) 上使用writer.writerow(row) for循环