Python CSV search
本问题已经有最佳答案,请猛点这里访问。
我对python还比较陌生,遇到了一个我认为很奇怪的问题。我正在搜索csvfile的第一列。
我使用的是csv模块,并且有一些嵌套for循环的代码。我的意图是让中间的循环在每次找到匹配项时从csvfile的第一行重新启动。但是它总是从csv中搜索的最后一行开始。
我下面的代码和结果将使我的问题更加明显。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | number = [1,3,5,6,7,8,1234,324,5,2,35] import csv with open('...../Documents/pycharm/testcsv.csv', 'rb') as csvimport: csvfile = csv.reader(csvimport) for num in number: print 'looking for ' + str(num) is_in_file = False print 'set to false' for row in csvfile: print 'looking at value ' + row[0] if row[0] == str(num): is_in_file = True print 'match, set to true' break print 'test1' if is_in_file == False: print str(num) + ' not found in file!' |
以下是在IDE中打印的内容:
1 2 3 4 5 6 7 8 | looking for 1 set to false looking at value a looking at value 1 match, set to true test1 looking for 3 set to false |
注意:这里我想看看csvfile的第一行(值A)。而是查看csvfile的第三行(值"")。
1 2 3 4 5 6 7 | looking at value looking at value 1234 looking at value 7 looking at value 1 looking at value 3 match, set to true test1 |
从这里开始,它跳过了我的内部循环,因为它已经通过了最后一行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | looking for 5 set to false looking at value 5 match, set to true test1 looking for 6 set to false looking at value 6 match, set to true test1 looking for 7 set to false looking at value 77 looking at value 23 looking at value 87 test1 7 not found in file! looking for 8 set to false test1 8 not found in file! looking for 1234 set to false test1 1234 not found in file! looking for 324 set to false test1 324 not found in file! looking for 5 set to false test1 5 not found in file! looking for 2 set to false test1 2 not found in file! looking for 35 set to false test1 35 not found in file! |
这是CSV文件
1 2 3 4 5 6 7 8 9 10 11 12 | a,c,b,d,e 1,3,4,5,6 ,7,7,,87 1234,1,98,7,76 7,8,90,0,8 1,3,98,0,0 3,cat,food,20,39 5,%,3,6,90 6,2,2,2,3 77,3,4,3,5 23,3,4,3,6 87,5,5,5, |
csvfile是一个生成器:第一次它将工作,第二次它将立即返回。考虑这样做:
1 | csvfile = list(csv.reader(csvimport)) |
然后你可以随心所欲地扫描
但是,当您对文件执行线性扫描时,这段代码实际上并没有被执行。考虑做一本字典。以下是如何
1 2 3 | d = dict() for r in csvfile: d[r[0]] = r[1:] |
然后,将内部循环替换为:
1 2 3 | if num in d: is_in_file = True print("%d is in file" % num) |