This code reads two CSV files, compares their contents row by row, and identifies any differences between them. If the files are identical, it prints a message indicating that. If differences are found, it prints the row number and column number where the differences occur.
Python Code:
import csv def compare_csv(file1, file2): with open(file1, 'r') as f1, open(file2, 'r') as f2: reader1 = csv.reader(f1) reader2 = csv.reader(f2) csv1 = list(reader1) csv2 = list(reader2) if csv1 == csv2: print("Files are identical.") else: print("Files are different.") for row_num, (row1, row2) in enumerate(zip(csv1, csv2), start=1): if row1 != row2: print(f"Difference found in row {row_num}:") for col_num, (col1, col2) in enumerate(zip(row1, row2), start=1): if col1 != col2: print(f"Column {col_num}: '{col1}' != '{col2}'") # Replace 'file1.csv' and 'file2.csv' with the paths to your CSV files compare_csv('file1.csv', 'file2.csv')
0 Comments