如果满足条件,则从csv文件中打印行

时间:2017-06-16 05:58:36

标签: csv

如果在以下代码中满足条件,我如何从csv文件中打印行?

import csv
with open('eggs.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',' )
for row in spamreader:
    s = (', '.join(row))
    if any("Open Positions, " in s): 
    # gives TypeError: 'bool' object is not iterable !!!
        print(s)

在没有if any("....语句的情况下,代码打印得非常好。

1 个答案:

答案 0 :(得分:0)

知道了!代码是:

import csv

key = ['Open Positions', 'Status']

with open('eggs.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',' )

for row in reader:

    # print(row)  # Check to see content of csv in list

    s=row[0:2]    # number of columns against key
    if s == key:
        print(row)