title: Combine columns from several CSV files into a single file
body: <p>I have a bunch of CSV files (only two in the example below). Each CSV file has 6 columns. I want to go into each CSV file, copy the first two columns and add them as new columns to an existing CSV file.</p>
  
  <p>Thus far I have:</p>
  
  <pre><code>import csv
  
  f = open('combined.csv')
  data = [item for item in csv.reader(f)]
  f.close()
  
  for x in range(1,3): #example has 2 csv files, this will be automated
      n=0
      while n&lt;2:
          f=open(str(x)+".csv")
          new_column=[item[n] for item in csv.reader(f)]
          f.close()
          #print d
  
          new_data = []
  
          for i, item in enumerate(data):
              try:
                  item.append(new_column[i])
                  print i
              except IndexError, e:
                  item.append("")
              new_data.append(item)
  
          f = open('combined.csv', 'w')
          csv.writer(f).writerows(new_data)
          f.close()
          n=n+1
  </code></pre>
  
  <p>This works, it is not pretty, but it works.
  However, I have three minor annoyances:</p>
  
  <ol>
  <li><p>I open each CSV file twice (once for each column), that is hardly elegant</p></li>
  <li><p>When I print the <code>combined.csv</code> file, it prints an empty row following each row?  </p></li>
  <li><p>I have to provide a <code>combined.csv</code> file that has at least as many rows in it as the largest file I may have. Since I do not really know what that number may be, that kinda sucks</p></li>
  </ol>
  
  <p>As always, any help is much appreciated!!</p>
  
  <p>As requested:
  1.csv looks like (mock data)</p>
  
  <pre><code>1,a
  2,b
  3,c
  4,d
  </code></pre>
  
  <p>2.csv looks like</p>
  
  <pre><code>5,e
  6,f
  7,g
  8,h
  9,i
  </code></pre>
  
  <p>the combined.csv file should look like</p>
  
  <pre><code>1,a,5,e
  2,b,6,f
  3,c,7,g
  4,d,8,h
  ,,9,i
  </code></pre>