我想将电子表格 (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 输出拆分,即 row[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)
最后 - 正在寻找让我清楚的 type() - 像往常一样,我太努力了。
elif qu == "other":
lisst = ochklist
if row[4] not in lisst:
#print("eureka")
#print(rowEdit)
writer.writerow(rowEdit)
感谢您帮助我完成它。!