title: How do I create a CSV file from database in Python?
body: <p>I have a Sqlite 3 and/or MySQL table named "clients"..</p>
  
  <p>Using python 2.6, How do I create a csv file named Clients100914.csv with headers? 
  excel dialect...</p>
  
  <p>The Sql execute: select * only gives table data, but I would like complete table with headers. </p>
  
  <p>How do I create a record set to get table headers. The table headers should come directly from sql not written in python. </p>
  
  <pre><code>w = csv.writer(open(Fn,'wb'),dialect='excel')
  #w.writelines("header_row")
  #Fetch into sqld
  w.writerows(sqld)
  </code></pre>
  
  <p>This code leaves me with file open and no headers. Also cant get figure out how to use file as log.  </p>