假设,我们有一个 listdata = [23, 511, 62]
而我们要检查这个列表是否存在于一个 csv
档,并找出符合的人的名字。
name,age,height,weight bob,24,6,82 ash,23,511,62 mary,22,62,55
我们怎样才能把它读到内存中去呢?csv.DictReader
并核对信息,如果符合的话,就打印出它的名字。
我不知道怎么能把整个... listdata
变量与字典中的值(csv.DictReader
),因为我知道如何使用字典的唯一方法是用key来访问它们,而key在这里是非常有限的,因为它不能把整个列表和同一条线上的其他键进行比较。
import csv
listdata = [23, 511, 62]
with open('file.csv', newline='') as csvfile:
reader = list(csv.reader(csvfile, delimiter=',', quotechar='|'))
# we remove the first row because it contains headers
for row in reader[1:]:
row = list(row)
if listdata == row[1:]:
print(row[0])
break