This type of question has been asked several times but I cant seem to find the exact same scenario and be using python 3.(3.5 in my case)
I have two files txt or csv. I need to compare each row and output the differences to a new lines in a new file.
Here is what I have tried so far: which is close but I cant figure out how to make each row difference a new line, i seem to only be able to make each word a new line or everything on one line.
a = open('test1.txt').read().split()
b = open('test2.txt').read().split()
c = [x for x in b if x not in a]
open('test3.txt', 'wt').write('\n'.join(c)+'\n')
the \n before the .join makes every word a new row, I don't want each difference a new row, I want all the differences from one row on the same row. I hope that makes sense.
Example: test1.txt:
how are you
I am well
all is good
test2.txt:
how are you
I like toys
all is not well
output: test3.txt
am well
good
I have also tried this code for CSV: but I cant an error.
import csv
f1 = open ("test1.csv")
oldFile1 = csv.reader(f1)
oldList1 = []
for row in oldFile1:
oldList1.append(row)
f2 = open ("test2.csv")
oldFile2 = csv.reader(f2)
oldList2 = []
for row in oldFile2:
oldList2.append(row)
f1.close()
f2.close()
print [row for row in oldList1 if row not in oldList2]
I get this error: I think its related to me being on version 3.5 and this code was written for 2.7?
File "test3.py", line 18
print [row for row in oldList1 if row not in oldList2]
^
SyntaxError: Missing parentheses in call to 'print'
Thank you for your help