Apoorve Bhardwaj
Apoorve Bhardwaj

Reputation: 21

Comparing contents in two csv file line by line using python

Hi I need to do following steps while comparing two csv files using python:

0) open file 1 and file 2

1) read one line from file 1

2) read one line from file 2

3) compare contents in each of line and count number of same and different

4) if contents are different, write contents in file 1 and file 2 in an output file

5) go back to step 1) before reaching the end of file

6) show record count of same contents and different contents, and close the files

Please suggest an easy to understand code for this. Thanks

currently my code is like this:

input_file1 = "Claim_ExportStudyPeriod_NoQuotes_Ap.csv"
input_file2 = "Claim_ExportStudyPeriod_NoQuotes_Jp.csv"
output_path = "Comparison.csv"

t1 = open(input_file1, 'r')

t2 = open(input_file2, 'r')

fileone = t1.readlines()
filetwo = t2.readlines()

with open(input_file2, 'r') as t2, open(output_path, 'w') as outFile:
    for line in t2:
        if line not in fileone:         
            outFile.write(line)

Upvotes: 1

Views: 133

Answers (1)

Aivar Paalberg
Aivar Paalberg

Reputation: 5167

Maybe something along those lines:

with open('first.csv', 'r') as f, open('second.csv', 'r') as s, open('out.csv', 'w') as o:
    diffs = 0
    for i, (first, second) in enumerate(zip(f, s), start=1):
        if first != second:
            print((f'row #{i}\n'
                   f'in first file: {first.strip()}\n'
                   f'in second file: {second.strip()}'), file=o)
            diffs += 1
    print(f'Different values on {diffs} row(s), same values on {i-diffs} row(s)')

Upvotes: 1

Related Questions