如果匹配,则跳过写入Csv的值

时间:2016-03-09 21:32:48

标签: python csv

我试图将列表中的一些字符串写入csv文件。我逐步浏览列表,并将其写入csv文件,但是当我在2个列表中找到匹配项时,我不想将列表中的值写入csv文件。如果该值与单独列表中的另一个值匹配,是否有人可以帮助我使用语法跳过将列表的值写入csv?如果你想看看我的语法出错了,我的代码如下。

import csv

listOfAppIDs = []
#open file to read each line and add each line in file to an array. These are our appID's to search
with open('filelcation/listAppIDs.txt', 'r') as fi:
    for line in fi:
        listOfAppIDs.append(line.rstrip('\n'))
listOfAppIDs.sort() #alphebetically sort list
print listOfAppIDs

alreadyReviewedAppIDs = ['youseemore','streampix','streamaudio','spotify','speedtest','soundcloud-base','smartsheet-base','samsung-updates','print-over-ms-smb','exp','dicom','pop3','x11','echo','webdav','ustream','telnet','skydrive-base','rsync','outlook-web','nfs','ms-office365-base','itunes-appstore','apple-appstore','aim-mail','discard','time','akamai-client','bomgar','cisco VPN','DHCP','IBM-Clearquest','Ironmountain-Connected','MS-SCOM','MS-SSTP','SCCP','SNMPV3','WCCP','XDMCP','secureserver-mail','teamviewer-base']
print alreadyReviewedAppIDs

i=0
i2=3
with open ('filelocation/outputAppIDsToDo.csv', 'wb') as outputFile:
    writer = csv.writer(outputFile, delimiter=',',)
    for item in listOfAppIDs:
        if(i%14==0):
            i2 +=1
        #for item2 in alreadyReviewedAppIDs:
        #   if (item2==item):
        #       break
        writer.writerow([i2, item])
        i += 1

1 个答案:

答案 0 :(得分:1)

更改此部分

with open ('filelocation/outputAppIDsToDo.csv', 'wb') as outputFile:
    writer = csv.writer(outputFile, delimiter=',',)
    for item in listOfAppIDs:
        if(i%14==0):
            i2 +=1
        if item in alreadyReviewedAppIDs: # if match
           continue # skips the item
        writer.writerow([i2, item])
        i += 1

你非常接近,continue语句跳转到forloop的下一次迭代 但是break语句只会停止当前循环并继续执行代码(循环)