Suppose i have Columns in file1.csv as
Customer id Name
Q1 Alen
W2 Ricky
E3 Katrina
R4 Anya
T5 Leonardo
and Columns in file2.csv as
Customer id Name
Q1 Alen
W2 Harry
E3 Katrina
R4 Anya
T5 Leonard
here as you can see for Customer id: W2 the corresponding name is not matching. so the output.csv should be like below:
Customer id Status
Q1 Matching
W2 Not matching
E3 Matching
R4 Matching
T5 Matching
How can i get the above output using python.
P.S. whats the code for comparing multiple columns, not just column Name?
My code
import csv
with open('file1.csv', 'rt', encoding='utf-8') as csvfile1:
csvfile1_indices = dict((r[1], i) for i, r in enumerate(csv.reader(csvfile1)))
with open('file2.csv', 'rt', encoding='utf-8') as csvfile2:
with open('output.csv', 'w') as results:
reader = csv.reader(csvfile2)
writer = csv.writer(results)
writer.writerow(next(reader, []) + ['status'])
for row in reader:
index = csvfile1_indices.get(row[1])
if index is not None:
message = 'matching'
writer.writerow(row + [message])
else:
message = 'not matching'
writer.writerow(row + [message])
results.close()
This is working fine, but can i write in any other easier way to get the same output? and what changes do i need to make to compare multiple columns?