title: compare columns in two csv files and extract the matched column in a third csv file
body: <p>I would like to compare columns in two csv files and extract the matched values as the following:</p>
  
  <p><strong>file1.csv is</strong></p>
  
  <p>115.06603,  5.9</p>
  
  <p>114.74721,  5.4</p>
  
  <p>114.85107,  6.2</p>
  
  <p>111.17744,  5.5</p>
  
  <p>192.77787,  3.2</p>
  
  <p>189.70226,   5</p>
  
  <p>0.46762,            3.7</p>
  
  <p>2.21539,            3.5</p>
  
  <p>2.96667,            3.6</p>
  
  <p><strong>And file2.csv is</strong></p>
  
  <p>115.06603</p>
  
  <p>115.06603</p>
  
  <p>114.74721</p>
  
  <p>114.74721</p>
  
  <p>114.74721</p>
  
  <p>114.74721</p>
  
  <p>114.85107</p>
  
  <p>114.85107</p>
  
  <p>114.85107</p>
  
  <p>114.85107</p>
  
  <p>114.85107</p>
  
  <p>111.17744</p>
  
  <p>111.17744</p>
  
  <p><strong>The output file file3.csv should be</strong></p>
  
  <p>115.06603,      5.9</p>
  
  <p>115.06603,      5.9</p>
  
  <p>114.74721,      5.4</p>
  
  <p>114.74721,      5.4</p>
  
  <p>114.74721,      5.4</p>
  
  <p>114.74721,      5.4</p>
  
  <p>114.85107,      6.2</p>
  
  <p>114.85107,      6.2</p>
  
  <p>114.85107,      6.2</p>
  
  <p>114.85107,      6.2</p>
  
  <p>114.85107,      6.2</p>
  
  <p>111.17744,      5.5</p>
  
  <p>111.17744,      5.5</p>
  
  <p>I used the following code but the output file give only the first column not the two columns. Could you help me in this problem?</p>
  
  <pre><code>&gt;&gt;&gt; with open("file1.csv", "rb") as in_file1, open("file2.csv", "rb") as in_file2,    open("file3.csv", "wb") as out_file:
  ...   reader1 = csv.reader(in_file1)
  ...   reader2 = csv.reader(in_file2)
  ...   writer = csv.writer(out_file)
  ...   for row2 in reader2:
  ...     for row1 in reader1:
  ...       if row2[0] == row1[0]:
  ...         row2[1] = row1[1]
  ...     writer.writerow(row2)
  </code></pre>
  
  <p><strong>Edit</strong>
  I used your code but the first part give the following error:</p>
  
  <pre><code>data1 = {}
  with open("file1.csv", "rb") as in_file1:
  ...   reader1 = csv.reader(in_file1)
  ...   for row1 in reader1:
  ...     data1[row1[0]] = row1[1]
  ... 
  Traceback (most recent call last):
  File "&lt;stdin&gt;", line 4, in &lt;module&gt;
  IndexError: list index out of range
  </code></pre>
  
  <p>As the separator in the file1.csv is ; not , I added delimiter=';' as following</p>
  
  <pre><code>data1 = {}
  with open("file1.csv", "rb") as in_file1:
  ...   reader1 = csv.reader(in_file1, delimiter=';')
  ...   for row1 in reader1:
  ...     data1[row1[0]] = row1[1]
  Traceback (most recent call last):
  File "&lt;stdin&gt;", line 4, in &lt;module&gt;
  IndexError: list index out of range
  </code></pre>
  
  <p>The same error as you see</p>
  
  <p>I added in.file.seek(0) as following </p>
  
  <pre><code>data1 = {}
  with open("file1.csv", "rb") as in_file1:
  ...   reader1 = csv.reader(in_file1)
  ...   for row1 in reader1:
  ...     in_file1.seek(0)
  ...     data1[row1[0]] = row1[1]
  ... 
  Traceback (most recent call last):
  File "&lt;stdin&gt;", line 5, in &lt;module&gt;
  IndexError: list index out of range
  </code></pre>
  
  <p>the same error. What is the problem? I am so depressed.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>the code that I used to remove empty rows</p>
  
  <pre><code>with open("file2.csv", "r") as in_file2, open("out.csv", "w") as out_file:
  ...  reader2 = csv.reader(in_file2)
  ...  writer = csv.writer(out_file)
  ...  for row in reader2:
  ...    if any(field.strip() for field in row):
  ...      writer.writerow(row)
  </code></pre>