我需要将整数值列表与CVS文件中的单行进行比较,以找到与该值匹配的行。
'''
firstScore = 90
secondScore = 80
thirdScore = 75
list = [firstScore, secondScore, thirdScore]
'''
和csv数据是:
Name,first,second,third
Paul,40,60,30
Kevin,90,80,75
Jenny,80,75,90
实际输出应为与以下三个值匹配的名称:Kevin
。
欢迎使用StackOverflow :)您可以尝试这个吗!
import csv
list = [firstScore, secondScore, thirdScore]
with open('test.csv', 'rt') as f:
reader = csv.reader(f, delimiter=',')
# skip the header of your csv
next(reader)
for row in reader:
if((list[0] == int(row[1])) and (list[1] == int(row[2])) and (list[2] == int(row[3]))):
# print name (present in first column -> index 0 of the row)
print(row[0])
break
else:
print("No match found..")
我不是Python方面的专家,但是如果它有效,请毫不犹豫地接受作为答案:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句