我尝试使用Python代码比较两个CSV文件。但我的代码并没有显示所有的不匹配。它只会显示每行的第一个不匹配。我需要特定行中的所有不匹配。
Python代码:
import csv, itertools
column_names = ['id','name','amount']
source_data = csv.reader(open('src.csv'))
target_data = csv.reader(open('tgt.csv'))
counter = 1
def rowElementCompare(sourceRow, targetRow):
row_length = min(len(sourceRow), len(targetRow))
for i in range(row_length):
if sourceRow[i] != targetRow[i]:
print i
return i
return None
for source_row,target_row in itertools.izip(source_data,target_data):
comparison_result = None
comparison_result = rowElementCompare(source_row, target_row)
#print (comparison_result)
if comparison_result != None: #comparison_result is the column index at which the mismatch occured
print "Mismatch in column %s on row number %d , source value %s, target value %s" % (column_names[comparison_result], counter, source_row[comparison_result], target_row[comparison_result])
counter += 1
文件1:
id,name,amount
1,bob,20
3,eva,8
3,sarah,7
4,jeff,19
6,fred,10
文件2:
id,name,amount
1,bob,23
3,sarah,7
4,jeff,19
5,mira,81
6,fred,13
输出我的代码:
Mismatch in column amount on row number 2 , source value 20, target value 23
Mismatch in column name on row number 3 , source value eva, target value sarah
Mismatch in column id on row number 4 , source value 3, target value 4
Mismatch in column id on row number 5 , source value 4, target value 5
Mismatch in column amount on row number 6 , source value 10, target value 13
预期产出:
Mismatch in column amount on row number 2 , source value 20, target value 23
Mismatch in column name on row number 3 , source value eva, target value sarah
Mismatch in column id on row number 4 , source value 3, target value 4
Mismatch in column name on row number 4 , source value sarah, target value jeff
Mismatch in column age on row number 4 , source value 7, target value 19
Mismatch in column id on row number 5 , source value 4, target value 5
Mismatch in column name on row number 5 , source value jeff, target value mira
Mismatch in column age on row number 5 , source value 19, target value 81
...
答案 0 :(得分:1)
问题是你每行只调用rowElementCompare
一次。此外,重复调用它将无济于事,因为它总是从行的开头开始,一旦找到第一个不匹配就停止。
解决此问题的一种方法是将rowElementCompare
更改为yield
其结果,而不是将其返回。这样你就可以遍历那一行中的所有不匹配。
这是更新的代码。更改的行标有# UPDATED
。
import csv, itertools
column_names = ['id','name','amount']
source_data = csv.reader(open('foo1.csv'))
target_data = csv.reader(open('foo2.csv'))
counter = 1
def rowElementCompare(sourceRow, targetRow):
row_length = min(len(sourceRow), len(targetRow))
for i in range(row_length):
if sourceRow[i] != targetRow[i]:
print i
yield i # UPDATED
return # UPDATED
for source_row,target_row in itertools.izip(source_data,target_data):
comparison_result = None
for comparison_result in rowElementCompare(source_row, target_row): # UPDATED
print "Mismatch in column %s on row number %d , source value %s, target value %s" % (column_names[comparison_result], counter, source_row[comparison_result], target_row[comparison_result])
counter += 1
清理代码的另一个小建议:你可以使用枚举来避免手动更新计数器变量。
for counter,(source_row,target_row) in enumerate(itertools.izip(source_data,target_data), start=1):