我对python很新,遇到了一个我认为很奇怪的问题。我正在搜索csvfile的第一列。
我正在使用csv模块并且有一些嵌套for循环的代码。我的意图是每次找到匹配时,中间的循环从csvfile的第一行重新启动。但它总是从csv中搜索的最后一行开始。
下面的代码和结果将使我的问题更加明显。
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中打印的内容:
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的第三行(值'')。
looking at value
looking at value 1234
looking at value 7
looking at value 1
looking at value 3
match, set to true
test1
从现在开始,它一直跳过我的内部循环,因为它已经通过了最后一行:
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!
这是csvfile
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,
答案 0 :(得分:0)
csvfile是一个生成器:第一次它会工作,但第二次它会立即返回。 考虑这样做:
csvfile = list(csv.reader(csvimport))
然后,您可以根据需要扫描csvfile
。
但是,当您对文件执行线性扫描时,此代码的效果并不高。考虑改为做字典。这是
的方式d = dict()
for r in csvfile:
d[r[0]] = r[1:]
然后,用:
替换你的内循环if num in d:
is_in_file = True
print("%d is in file" % num)