title: Python Script for comparing multiple columns in 2 csv files
body: <p>Hoping someone can help me or point me to a previous post with the correct info (I've been searching for a while without success),</p>
  
  <p>I am really new to python scripting and am spending the time studying to get my skills up, however I suddenly need to do the following whilst I am learning -  I am hoping this will help me understand Python a bit more whilst I am learning the basics elsewhere</p>
  
  <p>I have two CSV's with the same column data but different headers - example below</p>
  
  <pre><code>----$csv1------  
  ID, FirstName, Surname
  
  1, John, Smith
  
  2, Steve, Davis
  
  , John,Parrot,
  
  4, Dave,Smith
  
  5, Alan, Taylor
  
  ----$csv2------  
  Employee ID, First Name, Given Surname
  
  1, John, Smith
  
  2, Steven, Davis
  
  3, John, Parrott
  
  4, Dave, Allen
  
  6, Mike, Angelo
  </code></pre>
  
  <p>My script requirements are to compare the 2 csv's and create a 3rd file with the results (results.csv)</p>
  
  <ul>
  <li>If columns 1,2 &amp; 3 match then append row to results.csv with 'Correct'</li>
  <li>If columns 1 does not match but 2 &amp; 3 do then append row to results.csv with 'Wrong ID'</li>
  <li>If columns 2 does not match but 1 &amp; 3 do then append row to results.csv with 'Wrong Firstname'</li>
  <li>If columns 3 does not match but 1 &amp; 2 do then append row to results.csv with 'Wrong Surname'</li>
  <li>If entire row in $csv1 not in $csv2 then append row to results.csv with 'In CSV1 not CSV2'</li>
  <li>If entire row in $csv2 not in $csv1 then append row to results.csv with 'In CSV2 not CSV1'</li>
  </ul>
  
  <p>I know its a big ask but I'd be really grateful if anyone can provide a script with a bit of explanation to help me on my Python journey!</p>
  
  <p>Thanks all.</p>
  
  <p>----SCRIPT ADDED------</p>
  
  <pre><code>import csv
  CSV1_tuples = []
  CSV2_tuples = []
  
  with open("DB1.csv") as CSV2:
      csv_CSV2 = csv.reader(CSV2)
      for row in csv_CSV2:
          CSV2_tuples.append(tuple(row[0:3]))
  
  with open("DB2.csv") as CSV1:
      csv_CSV1 = csv.reader(CSV1)
      for row in csv_CSV1:
          CSV1_tuples.append(tuple(row[0:3]))
          if tuple(row[0:3]) in CSV2_tuples:
              print(( row[0:3] ), "In both DB1 &amp; DB2")
          if tuple(row[1:3]) in CSV2_tuples:
              print(( row[0:3] ), "Wrong ID")
  </code></pre>