0

I have two csv files generated from 2 different python scripts sample1.csv and sample2.csv.

sample1.csv has the following data:-

92    90    85
100   89    78
76    45    78
76    54    86

sample2.csv has the same data but also with headers:-

Maths    Science    English
92         90              85
100        89              78
76         45              78
76         54              86

My code works comparing the 2 csv files line by line. If there were no headers, then it would work perfectly. Assuming that there is no way to get rid of the header at the first place and will always be there, how can I skip that line so that my comparison would start from 1st line of the sample1.csv with 2nd line of sample2.csv and so on.

My code snippet:-

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)

It displays the result in format:-

Line(92,90,85) in sample1.csv NOT FOUND in sample2.csv

and so on....

sam
  • 127
  • 3
  • 13

1 Answers1

0

Perhaps try using something like:

if not line.startswith("Maths") : continue

Update:

try doing:

if line != "\n" and line in f2.pop(0): #this should remove the 1st line

or:

if line != "\n" and line in f2[1:]: #this should remove the 1st line

See this post...
I hope this helps.

Community
  • 1
  • 1
Renier
  • 1,523
  • 4
  • 32
  • 60
  • What if there are different headers next time, it starts with physics, chemistry? In short , the headers can vary each time when sample2.csv is formed – sam Jul 17 '14 at 10:36
  • its okay, basically it should ignore the first line of the sample2.csv everytime and starts the comparison from its second line – sam Jul 17 '14 at 10:39
  • pop(0), so as soon as I do f2.readlines(), i pop it out and then the same code works – sam Jul 17 '14 at 11:29