Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing and appending .csv files in Python
    primarykey
    data
    text
    <p>I have 2 .csv files which I want to compare and append. Let's say there are numbers in column 1 file 1. I have a similar set of numbers in column 1 file 2 -- but in a different order and not all are present. I want to search through file 1 column 1 to see if there is a match for the number in column 1 file 2. If there is a match, I want to append the information from file 2 into file 1 by adding the columns to the matched number. I also get rid of any blank lines in the input file.</p> <p>For example:</p> <pre><code>File 1 Number Name EmailAddress 1 Jenny jj@ymail.com 2 Josh jh@ymail.com 3 Tony ty@gmail.org 4 Jeff jf@youtube.cc File2 Number Address 4 123 Walnut St 1 17 Yerlington Wy 7 420 St B Output File Number Name EmailAddress Address 4 Jeff jf@youtube.cc 123 Walnut St 1 Jenny jj@ymail.com 17 Yerlington Wy 7 NO ENTRY </code></pre> <p>Here's the code I have so far, which seems to do everything but append the correct information.</p> <pre><code>import csv f1 = file('inFile.csv', 'rb') f2 = file('inFile2', 'rb') f3 = file('outFile.csv', 'wb') 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 end_row = masterlist if any(hosts_row): if hosts_row[0] == master_row[0]: results_row.append(end_row[row][1]) found = True break if not found: results_row.append('NO ENTRY') if any(hosts_row): c3.writerow(results_row) f1.close() f2.close() f3.close() </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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