如何将列表值与单个 csv 文件的行相匹配?
How can I match a list value to a single csv file's row?
我需要将整数值列表与 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
实际输出应该是匹配3个值的名字:Kevin
.
欢迎来到 Whosebug :)
你能试试这个吗?
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 方面的专家,但如果它有效,请毫不犹豫地接受作为答案:)
我需要将整数值列表与 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
实际输出应该是匹配3个值的名字:Kevin
.
欢迎来到 Whosebug :) 你能试试这个吗?
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 方面的专家,但如果它有效,请毫不犹豫地接受作为答案:)