cursor.execute('select * from table_you_want_to_turn_to_csv')
with open('output_file.csv', 'wb') as fout:
    writer = csv.writer(fout)
writer.writerow([i[0] for i in cursor.description])
writer.writerows(cursor.fetchall())
How to export a table to csv or excel format