Shairyar Shafqat
Shairyar Shafqat

Reputation: 117

Read two csv files and compare every line. If the lines match print both lines, if it isn't similar print invalid

import csv

f1 = open("file1.csv")

f2 = open("file2.csv")

csv_f1 = csv.reader(f1)

csv_f2 = csv.reader(f2)

for row1 in csv_f1: 
    for row2 in csv_f2:
        if row1 == row2:
            print row1[0], row2[0]
        else:
            print row1[0], "Invalid"

this program isn't printing out row1, row2. It's just printing first line of file1 and invalid on the same line several times.

Upvotes: 6

Views: 9067

Answers (2)

nwk
nwk

Reputation: 4050

You need to compare the corresponding rows, not each row with each row, which is what your code does right now.

from __future__ import print_function

import csv
import itertools
import sys

# Select the right function based on whether we are in Python 2 or 3.
if sys.version_info.major == 2:
    zip_longest = itertools.izip_longest
else:
    zip_longest = itertools.zip_longest

f1 = open("file1.csv")
f2 = open("file2.csv")

csv_f1 = csv.reader(f1)
csv_f2 = csv.reader(f2)

for row1, row2 in zip_longest(csv_f1, csv_f2):
    if row1 == row2:
        print(row1[0], row2[0])
    else:
        print(row1[0], "Invalid")

Upvotes: 11

Vivek Sable
Vivek Sable

Reputation: 10221

  1. Read two CSV files and create dictionary which has key as line number and value as row
  2. Iterator on file1 i.e. root1 and compare with file2 i.e. root2 with the same key.
  3. Handle exception when file1 has more entries then file2.

Code:

import csv
with open("/home/infogrid/Desktop/file1.csv", "rb") as fp1:
    root = csv.reader(fp1,)
    root1 = {}
    for i in root:
        root1[root.line_num] = i
    
with open("/home/infogrid/Desktop/file2.csv", "rb") as fp1:
    root = csv.reader(fp1,)
    root2 = {}
    for i in root:
        root2[root.line_num] = i

for i in root1:
    try:
        if root1[i]==root2[i]:
            print root1[i], root1[i]
        else:
            print root1[i], "Invalid"
    except:
        print root1[i], "Invalid"

Output:

['test1', 'test3', 'test4'] ['test1', 'test3', 'test4']
['test1', 'test5', 'test4'] Invalid
['test1', 'test3', 'test4'] ['test1', 'test3', 'test4']
['test1', 'test3', 'test4'] Invalid

Upvotes: 3

Related Questions