I currently have two CSVs. I'm trying to scan through each of them, compare the lines, and if there is a line in one that is not in the other, I'd like to print that line to a new CSV. As it stands now this is my code:
# compares the two files
with open('csv1.csv', 'r') as t1, open('csv2.csv', 'r') as t2:
fileone = t1.readlines()
filetwo = t2.readlines()
# scans through the two files and writes differences to new csv
with open('csv3.csv', 'w') as outFile:
for line in filetwo:
if line not in fileone:
outFile.write(line)
csv1 has 201 rows while csv2 has 156, so I would expect csv3 to have 45 results, however instead it has 156 (the entirety of csv2). I tried switching the logic around to
for line in fileone:
if line not in filetwo:
outfile.write(line)
but that just made csv3 empty.
Any help would be appreciated!
also the code I used was sourced from Python : Compare two csv files and print out differences
Here's an example of a line that appears in both but still shows up in csv3
,MAJOR,MAJOR_CODE,
0,Accountancy,ACCT,
1,Aerospace Engineering,AERO