I am trying to compare 2 CSV files, where first one only has couple of entries and the other one has 300 lines. The output I am trying to achive is to print the output of the line from the large file if column email from file1.csv matches the column email from file2.csv matches but the date doesn't. I would like to do this using the "CSV" module. Here is an example:
file1.csv (few entries):
Tom,Johns,[email protected],14 May 2017
Imaginary,User,[email protected],23 May 2018
file2.csv (large file):
First,User,[email protected],10 Jan 2018
Second,User,[email protected],13 Feb 2018
Tom,Johns,[email protected],16 Jun 2017
Imaginary,User,[email protected],23 May 2018
result.csv (Desired result)
Tom,Johns,[email protected],16 Jun 2017
I tried achieving this using the function "next" but due to only 2 entries in file1.csv the script stops. I have rewritten the code but now I am getting I/O operation on closed file.
import csv
with open('file1.csv', 'r') as first_csv:
dialect = csv.excel()
file1 = csv.reader(first_csv, dialect)
with open('file2.csv', 'r') as second_csv:
dialect = csv.excel()
file2 = csv.reader(second_csv, dialect)
writer = csv.writer(open('result.csv', 'w'))
output = set()
for row1 in file1:
for row2 in file2:
if (row1[2] == row2[2]) and (row1[3] != row1[3]):
writer.writerow(row2)
output.append(row2)
csv
code, I am just a in the middle of something. - Mr.Zeuswith
statement, thus closing the I/O stream when you reach the end of it. I fixed that but there is still an issue with your logic. - Mr.Zeus