title: Replacing content of column 'x' from file A with column 'x' in a very large file B
body: <p>I have two files. "A" which is not too large (2GB) and "B" which is rather large at 60GB. I have a primitive code as follows:</p>
  
  <pre><code>import csv #imports module csv
  
  filea = "A.csv"
  fileb = "B.csv"
  output = "Python_modified.csv"
  
  source1 = csv.reader(open(filea,"r"),delimiter='\t')
  source2 = csv.reader(open(fileb,"r"),delimiter='\t')
  #open csv readers
  
  source2_dict = {}
  
  # prepare changes from file B
  for row in source2:
      source2_dict[row[2]] = row[2]
  
  # write new changed rows
  with open(output, "w") as fout:
      csvwriter = csv.writer(fout, delimiter='\t')
      for row in source1:
          # needs to check whether there are any changes prepared
          if row[3] in source2_dict:
              # change the item
              row[3] = source2_dict[row[3]]
          csvwriter.writerow(row)
  </code></pre>
  
  <p>Which should read through column 3 from both files and replace column 4 in file A with the contents of column 4 in file B if there's a match. However since it's reading in the large files its very slow. Is there any way to optimize this?</p>