Python:将列表中的字段与黑名单字段进行比较

时间:2013-06-03 15:19:01

标签: python if-statement list-comprehension

我想将电子表格(CSV)中的行与优秀值列表[]进行比较,并创建坏行的黑名单(包含未出现在白名单中的值的任何行都会打印到一个文件)。

if any(item in row[4] for item in lisst):
    print(rowEdit) #scaffolding
    writer.writerow(rowEdit)

将所有内容与白名单中的项目进行匹配,并将其写入文件

if any(item in row[4] for item not in lisst):
    print(rowEdit)
    writer.writerow(rowEdit)

似乎应该打印出未列入白名单的文件部分,以便手动编辑添加到白名单。它没有。

我正在排序的数据是带有资产数据的CSV文件。我想通过OS输出拆分,这是行[4]列表项 压痕有点混乱......

    import csv

def main():
choice = "chew"
filename=raw_input("enter the filename==>  ")
while choice != "swallow":

    os_choice = raw_input('Enter  "1" for Linux,\n       "2" for Windows\n       "3" for all others and\n       "4" to exit the script =>  ')

    qu = ""
    if os_choice == "1":
        qu = "nix"
    elif os_choice == "2":
        qu = "win"
    elif os_choice == "3":
        qu = "other"
    elif os_choice == "4":
        choice = "swallow"
        break
    else: continue
    #t=titleblock(filename)
    #L=labels(filename)
    c=content(filename, qu)

#   print(t,'\n',c,'\n',r,'\n')
return 0

def content(filename, qu):

with open(qu+'_content_'+filename, 'wb') as content:
    writer = csv.writer(content)
    with open(filename, 'rb') as mycsv:
        reader = csv.reader(mycsv)
        counter = 0

        for counter,row in enumerate(reader):
            if counter < 7: continue
#               if counter > ([-2:]) : break # This string-slicing technique doesn't work on lists made by csv module for some reason
            rowEdit = [row[0],row[22],row[2], row[4], row[6], row[15], row[16], row[11], row[18], row[19], row[20], row[25], row[26], row[27], row[28], row[29], row[30], row[31]]
            chklist=["OS", "Linux 2.4-2.6 / Embedded Device", "Linux 2.4-2.6", "Linux 2.6", "Red Hat Enterprise Linux ES 4", "Red Hat Enterprise Linux Server 5.8", "Linux*"]
            chklist2 = "Linux"
            wchklist=["OS", "Windows Server 2003 Service Pack 2", "Windows Server 2008 R2 Enterprise 64 bit Edition Service Pack 1","Windows"]
            ochklist=chklist+wchklist
            if qu == "nix":
                lisst = chklist
                if any(item in row[4] for item in lisst):
                    #print(rowEdit)
                    writer.writerow(rowEdit)
            elif qu == "win":
                lisst = wchklist
                if any(item in row[4] for item in lisst):
                    #print(rowEdit)
                    writer.writerow(rowEdit)
            elif qu == "other": 
                lisst = ochklist
                splisst = set(lisst)
                #if any(item not in row[4] for item in lisst):
                if splisst & row[4].split():
                    print(rowEdit)
                    writer.writerow(rowEdit)

最后 - 正在寻找类型()让我清楚 - 像往常一样,我太努力了。

elif qu == "other": 
lisst = ochklist
if row[4] not in lisst:
    #print("eureka")
    #print(rowEdit)
    writer.writerow(rowEdit)

感谢您帮助我完成它。!

1 个答案:

答案 0 :(得分:3)

你把测试与循环混淆了;将not移动到生成器表达式中的in测试表达式:

if any(item not in row[4] for item in lisst):

for item lisst部分遍历lisst元素,生成器表达式的第一部分是实际测试。

根据row[4]的结构,将白名单设置为一组可能会更高效:

whitelist = set(lisst)

if whitelist.intersection(row[4].split()):
    # True if any whitespace separated value in `row[4]` is a member of whitelist too