title: how do you write output of sql to a csv file in python
body: <p>I am trying to connect to oracle table and execute a sql. I need to export result set to a csv file. My code is below:</p>
  
  <pre><code>import pyodbc
  import csv
  
  cnxn = pyodbc.connect("DSN=11g;UID=test101;PWD=passwd")
  cursor = cnxn.cursor()
  cursor.execute(sql)
  row = cursor.fetchall()
  with open('data.csv', 'w', newline='') as fp:
      a = csv.writer(fp, delimiter=',')
      for line in row:
          a.writerows(line)
  
  cursor.close()
  </code></pre>
  
  <p>when I do print to line within for loop, I get something like this:</p>
  
  <pre><code>('Production', 'farm1', 'dc1prb01', 'web')
  ('Production', 'farv2', 'dc2pr2db01', 'app.3')
  ('Production', 'farm5', 'dc2pr2db02', 'db.3')
  </code></pre>
  
  <p>this is not working. Any ideas what I might be missing?</p>