Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, you don't have separator in your csv files, I had to rewrite them like this (comma separator):</p> <p>inFile.csv:</p> <pre><code>Number,Name,EmailAddress 1,Jenny,jj@ymail.com 2,Josh,jh@ymail.com 3,Tony,ty@gmail.org 4,Jeff,jf@youtube.cc </code></pre> <p>inFile2.csv:</p> <pre><code>Number,Address 4,123 Walnut St 1,17 Yerlington Wy 7,420 St B </code></pre> <p>Then you have few problems in your code:</p> <ul> <li>you are missing the check for empty master_row</li> <li>strange variable end_row is doing something you don't want and is practically useless as you really want to append <code>master_row[1]</code></li> <li>condition for <code>not found</code> is in wrong block, so 'NO ENTRY' is appended multiple times</li> <li>there is unused variable <code>row</code></li> <li>(just enhancement) python will close files for you when their variables comes out of scope, so you can remove close() calls</li> </ul> <p>After my fixes:</p> <pre><code>import csv f1 = file('inFile.csv', 'rb') f2 = file('inFile2.csv', '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: found = False for master_row in masterlist: results_row = hosts_row if hosts_row and master_row: if hosts_row[0] == master_row[0]: results_row.append(master_row[1]) found = True break if not found: results_row.append('NO ENTRY') if any(hosts_row): c3.writerow(results_row) </code></pre> <p>output file:</p> <pre><code>Number,Name,EmailAddress,Address 1,Jenny,jj@ymail.com,17 Yerlington Wy 2,Josh,jh@ymail.com,NO ENTRY 3,Tony,ty@gmail.org,NO ENTRY 4,Jeff,jf@youtube.cc,123 Walnut St </code></pre> <p>HTH</p>
    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.
 

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