我想使用file1.csv作为file2.csv的查找。出现的任何内容都应打印file2.csv中的整行。你知道吗
但是,当我在file2.csv的行中循环并评估对数据的查找时,我无法使line变量等于我的第二列(row1)。我好像少了什么?你知道吗
import csv
import sys
file1 = 'file1.csv'
file2 = 'file2.csv'
appcode = []
with open(file1, "r") as f:
f.readline() # Skip the first line
for line in f:
appcode.append(str(line.strip("\n")))
print('This is what we are looking for from file1 ...' +line)
csv_file = csv.reader(open(file2, "rb"), delimiter=",") # was rb
#loop through csv list
for row in csv_file:
print('line = '+line +' '+'row is... '+row[1])
#if current rows 2nd value is equal to input, print that row
if str(line) is str(row[1]):
print row
else:
print 'thinks '+str(line)+'='+str(row[1])+' is false'
您可以稍微重新构造代码:
通过检查
row[1] in f_set
,您可以避免使用is
进行错误的比较—作为一般规则:仅当确实要检查两个对象是否是相同的对象时才使用—而不是当它们包含相同的对象时才使用。你知道吗输出(2.7):#删除
(
和)
处的print
使其更好输出(3.6):
读数:
https://docs.python.org/2/library/sets.html
相关问题 更多 >
编程相关推荐