python - Compare two csv files and write shared items to a new csv file -
i have 2 csv files formatted same way (two columns of data):
name link name link name link name link
the difference between them data within 2 columns (different names , different links). i'd find names , links appear in both csv files , write them new csv file. 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 row in c2] hosts_row in c1: row = 1 found = false 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 based on answer a similar question, realize format of csv files in case different. , error:
masterlist = [row row in c2] _csv.error: new-line character seen in unquoted field - need open file in universal-newline mode?
how adjust above code fit format csv files. or there better way this? appreciated i'm starting python , don't think i've grasped concept of comparing data in 2 files yet.
l1 = set(open('f1.csv')) l2 = set(open('f2.csv')) open('f3.csv', 'wb').writelines(l1 & l2)
l1
, l2
sets of lines in f1.csv , f2.csv respectively. l1 & l2
evaluates set intersection lines found in both files , outputs them f3.csv.
Comments
Post a Comment