Home > Software engineering >  compare two csv files and list the differences in python
compare two csv files and list the differences in python

Time:12-17

I am trying to compare and list the differences between two csv files in python, one of the file has a regex.

for example:
The file name file1.csv has the content as shown below

abc-*  
xyz-*  

The file name file2.csv has the content as shown below

abc-123  
test-658  

The output should print what is missing and what is new in file2.csv. for example it should list the differences as test-658, -xyz-*

note: both the files only have one column without the column name

CodePudding user response:

To compare and list the differences between two CSV files in Python, you can use the 'csv' module to read the contents of the files, and use sets and dictionaries to compare the values and identify the differences.

Here is an example of how you can implement this in Python:

import csv

# Read the contents of file1.csv into a set
with open('file1.csv', 'r') as f:
    reader = csv.reader(f)
    file1_values = set(row[0] for row in reader)

# Read the contents of file2.csv into a set
with open('file2.csv', 'r') as f:
    reader = csv.reader(f)
    file2_values = set(row[0] for row in reader)

# Find the values that are present in file2.csv but not in file1.csv
new_values = file2_values - file1_values

# Find the values that are present in file1.csv but not in file2.csv
missing_values = file1_values - file2_values

# Print the differences
for value in new_values:
    print(f' {value}')
for value in missing_values:
    print(f'-{value}')

I hope this helps :)

  • Related