title: Match 2 csv files and write the third cell of the second file
body: <p>I wrote this code to compare two <code>CSV files (f1 and f2)</code> which both have 3 columns and many rows, then each time an <code>item at cell 1 of f1 matches that of f2</code> and <code>item at cell 2 of f1 matches that of f2, it should write the values</code>cell1 of f1, cell2 of f1, cell 3 of f2, for each respective column to a file named <code>network_python.csv</code> </p>
  
  <p><strong>The code:</strong></p>
  
  <pre><code>t = {}
  with open('file1.csv') as ff:
      for f1 in csv.DictReader(ff):
          with open('file2.csv') as ff:
              for f2 in csv.DictReader(ff):
                  if int(f1['From'].strip()) == int(f2['From'].strip()) and int(f1['To'].strip()) == int(f2['To'].strip()):
                      print (f1['From'], f1['To'], f2['Mode'])
                      t.update({'From': f1['From'], 'To': f1['To'], 'Mode': f2['Mode']})
  
  with open('network_python.csv', 'w') as csvfile:
      fieldnames = ['From', 'To', 'Mode']
      writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
      writer.writeheader()
      for k,v in t.iteritems():
          writer.writerow(t)
  </code></pre>
  
  <p><strong>Sample data in file1.csv</strong></p>
  
  <pre><code>From    To  Mode
  1   2   cw
  2   1   cw
  3   4   cwt
  7   2   cbt
  8   9   ct
  </code></pre>
  
  <p><strong>Sample data in file2.csv</strong></p>
  
  <pre><code>From    To  Mode
  8   9   c
  3   4   cw
  1   2   cwt
  7   2   ct
  2   1   cb
  </code></pre>
  
  <p>The code works fine (ie gets the right output) but when writing to file it writes to one single row thereby overwriting the previous result. Also is there a way I can improve the code's efficiency? as it is quite slow with large files. I have searched some questions here but they don't exactly answer my question Thank you for your time </p>