我有2个csv文件text1.csv,其中包含: -
1 6 1 1 516405 0 21 8
1 6 1 1 516405 21 8
1 6 1 1 21 34
1 6 1 21 60
text2.csv包含相同的数据: -
1 6 1 1 516405 0 21 8
1 6 1 1 516405 21 8
1 6 1 1 21 34
1 6 1 21 60
比较它们csv文件并以某种方式显示输出的最佳方法是什么: -
Line1 in text1.csv found in Line1 text2.csv
Line2 in text1.csv found in Line2 text2.csv
依此类推,反之亦然。
由于
答案 0 :(得分:0)
f1_name = "text1.csv"
f2_name = "text2.csv"
f1 = open(f1_name,'r').readlines()
f2 = open(f2_name,'r').readlines()
count1 = 0
for line in f1:
count1+=1
if line != "\n" and line in f2:
print "Line{0} in {1} found in Line{2} in {3} ({4})".format(
str(count1),
f1_name,
str(1+f2.index(line)),
f2_name,
line.strip())
f1.close()
f2.close()
<强>输出强>
Line1 in text1.csv found in Line1 in text2.csv (1 6 1 1 516405 0 21 8)
Line3 in text1.csv found in Line3 in text2.csv (1 6 1 1 516405 21 8)
Line5 in text1.csv found in Line5 in text2.csv (1 6 1 1 21 34)
Line7 in text1.csv found in Line7 in text2.csv (1 6 1 21 60)