Reputation: 39
Suppose, we have a list of listdata = [23, 511, 62]
and we want to check whether this list exist in a csv
file and find out the name of the person who matches it
name,age,height,weight bob,24,6,82 ash,23,511,62 mary,22,62,55
How can we do so by reading it into memory using csv.DictReader
and checking the information if it matches print out it's name
I don't know how can I compare the whole listdata
variable with the values in dictionary (csv.DictReader
) as only way I know how to use dictionaries is by accessing them with key and key here is pretty limited as it can't take the whole list and compare with other keys in a same line/row.
Upvotes: 2
Views: 258
Reputation: 1732
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
Upvotes: 1