我有2个不同的python脚本sample1.csv和sample2.csv生成的两个csv文件。
sample1.csv具有以下数据: -
92 90 85
100 89 78
76 45 78
76 54 86
sample2.csv具有相同的数据,但也包含标题: -
Maths Science English
92 90 85
100 89 78
76 45 78
76 54 86
我的代码可以逐行比较2个csv文件。如果没有标题,那么它将完美地工作。假设在第一个地方没有办法摆脱标题并且总是在那里,我怎么能跳过那一行,这样我的比较就会从sample1.csv的第一行开始,第二行是sample2.csv和等等。
我的代码段: -
import csv
import sys
list1 = ["sample1.csv"]
list2 = ["sample2.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}".format(
str(result),
f1_name,
str(1+f2.index(line)),
f2_name)
else:
if line != "\n":
print "Line({0}) in {1} NOT FOUND in {2}".format(
line.strip("\n"),
f1_name,
f2_name)
以格式显示结果: -
Line(92,90,85) in sample1.csv NOT FOUND in sample2.csv
依旧......
答案 0 :(得分:0)
也许尝试使用类似的东西:
if not line.startswith("Maths") : continue
更新
尝试做:
if line != "\n" and line in f2.pop(0): #this should remove the 1st line
或:
if line != "\n" and line in f2[1:]: #this should remove the 1st line
请参阅此post ...
我希望这会有所帮助。