Note that there are some explanatory texts on larger screens.

plurals
  1. POCompare two csv files and write shared items to a new csv file
    primarykey
    data
    text
    <p>I have two csv files formatted the same way (two columns of data):</p> <pre><code> Name Link Name Link Name Link Name Link </code></pre> <p>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:</p> <pre><code> 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() </code></pre> <p>This is based on an answer to <a href="https://stackoverflow.com/questions/5268929/python-comparing-two-csv-files-and-searching-for-similar-items">a similar question</a>, however I realize the format of the csv files in that case is different. And so I get this error:</p> <pre><code> 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? </code></pre> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload