以下是代码段: -
import csv
import sys
f1_name = "Result1.csv"
f2_name = "Result2.csv"
f1 = open(f1_name,'r').readlines()
f2 = open(f2_name,'r').readlines()
count1 = 0
for line in f1:
result = line.strip("\n")
count1+=1
if line != "\n" and line in f2:
print "Line({0}) in {1} found in Line{2} in {3}({4})".format(
str(result),
f1_name,
str(1+f2.index(line)),
f2_name,
line.strip())
到目前为止,代码正在比较两个csv文件并以下列格式生成输出: -
Line(1 6 1 1 516405 0 21 8 ) in Result1.csv found in Line2 in Result2.csv(1 6 1 1 516405 0 21 8)
Line(1 6 1 1 516405 21 8 ) in Result1.csv found in Line4 in Result2.csv(1 6 1 1 516405 21 8)
Line(1 6 1 1 21 34 ) in Result1.csv found in Line6 in Result2.csv(1 6 1 1 21 34)
Line(1 6 1 21 60 ) in Result1.csv found in Line8 in Result2.csv(1 6 1 21 60)
但这只是两个文件之间的比较。我有50个csv文件要与50个csv文件进行比较。
所以我有两个清单: -
list1 = [Result1.csv, Result2.csv...Result50.csv]
list2 = [Gen1.csv, Gen2.csv....Gen.csv]
如何修改我的代码并将Result1.csv与Gen1.csv,Result2.csv与Gen2.csv进行比较等等?
所以期望的输出将是相同的,但它将有一个标题,如: -
Comparing Result1.csv to Gen1.csv
(Same output mentioned above)
Comparing Result2.csv to Gen2.csv
(output)
依旧......
答案 0 :(得分:0)
将两个列表拉链并执行
import csv
import sys
list1 = ['Result1.csv', 'Result2.csv'...'Result50.csv']
list2 = ['Gen1.csv', 'Gen2.csv'....'Gen.csv']
for i,j in zip(list1,list2):
f1_name = i
f2_name = j
f1 = open(f1_name,'r').readlines()
f2 = open(f2_name,'r').readlines()
count1 = 0
for line in f1:
result = line.strip("\n")
count1+=1
if line != "\n" and line in f2:
print "Line({0}) in {1} found in Line{2} in {3}({4})".format(
str(result),
f1_name,
str(1+f2.index(line)),
f2_name,
line.strip())
else:
if line != "\n":
print line,'-this is not found in file2'
f1.close()
f2.close()