I have two csv files formatted the same way (two columns of data):
Name Link
Name Link
Name Link
Name Link
The only difference between them is the data within those two columns (different names and different links). I'd like to find the names and links that appear in both csv files and write them to a new csv file. So far I've tried:
import csv
f1 = file('/path/to/f1.csv', 'r')
f2 = file('/path/to/f2.csv', 'r')
f3 = file('/path/to/f3.csv', 'w')
c1 = csv.reader(f1)
c2 = csv.reader(f2)
c3 = csv.writer(f3)
masterlist = [row for row in c2]
for hosts_row in c1:
row = 1
found = False
for master_row in masterlist:
results_row = hosts_row
if hosts_row[3] == master_row[1]:
results_row.append('FOUND in master list (row ' + str(row) + ')')
found = True
break
row = row + 1
if not found:
results_row.append('NOT FOUND in master list')
c3.writerow(results_row)
f1.close()
f2.close()
f3.close()
This is based on an answer to a similar question, however I realize the format of the csv files in that case is different. And so I get this error:
masterlist = [row for row in c2]
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
How do I adjust the above code to fit the format for my csv files. Or is there a better way to do this? Any help would greatly be appreciated as I'm just starting with python and I don't think I've completely grasped the concept of comparing data in two files yet.