title: Python - CSV module to write certain rows to new file
body: <p>I've got a small issue. I'm trying to create a script that takes large (~2gb) csv files (id, integer, integer), sorts them by the first integer and then writes, to a new file, the top x rows (as defined by the user).</p>
  
  <p>I'm able to get the sort function to work as required and extracting the top X rows works also but I can't work out how to get this output to write to a csv.
  To check it has been working, I have included a print function and it all seems to work out fine.</p>
  
  <p>I feel like I'm missing a really basic concept in the csv module but I can't work out what it is!</p>
  
  <pre><code>import csv
  import operator
  
  def csv_to_list(csv_file, delimiter=','):
  
      with open(csv_file, 'r') as csv_con:
          reader = csv.reader(csv_con, delimiter=delimiter)
          return list(reader)
  
  def sort_by_column(csv_cont, col, reverse=True):
  
      header = csv_cont[1]
      body = csv_cont[1:]
      if isinstance(col, str):  
          col_index = header.index(col)
      else:
          col_index = col
      body = sorted(body, 
             key=operator.itemgetter(col_index), 
             reverse=reverse)
      #body.insert(0, header)
      return body
  
  def print_csv(csv_content):
      for row in csv_content:
          row = [str(e) for e in row]
          print('\t'.join(row))
  
  def write_csv(dest, csv_cont):
      with open(dest, 'w') as out_file:
          writer = csv.writer(out_file, delimiter=',')
          for row in csv_cont:
              writer.writerow(row)
  
  csv_cont = csv_to_list(input_hep.csv)
  row_count = sum(1 for row in csv_cont)
  num_rows = int(input("Skim size?: "))
  output_file = input("Output: ")
  
  csv_sorted = sort_by_column(csv_cont, 1)
  for row in range(num_rows):
      print(csv_sorted[row])
  </code></pre>
  
  <p>My main idea was to try:</p>
  
  <pre><code>with open(output_file+'.csv','w') as f:
      writer = csv.writer(f, delimiter =',')
      for row in range(num_rows):
          writer.writerow(row)
  </code></pre>
  
  <p>But then I get a "_csv.Error: iterable expected, not int" error. I get why but I'm struggling to understand how I can get the output (as it is printed) to write within a csv.
  Any tips or pointers would be appreciated.</p>