def dump_csv(query, file_or_name, include_header=True, close_file=True, append=True, csv_writer=None):
 if isinstance(file_or_name, basestring):
  fh = open(file_or_name, ((append and 'a') or 'w'))
 else:
  fh = file_or_name
  if append:
   fh.seek(0, 2)
 writer = (csv_writer or csv.writer(fh, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL))
 if include_header:
  header = []
  for (idx, node) in enumerate(query._select):
   if node._alias:
    header.append(node._alias)
   elif isinstance(node, (Field, Func)):
    header.append(node.name)
   else:
    header.append(('col_%s' % idx))
  writer.writerow(header)
 for row in query.tuples().iterator():
  writer.writerow(row)
 if close_file:
  fh.close()
 return fh

def dump_csv ( query , file_or_name , include_header = True , close_file = True , append = True , csv_writer = None ) : if isinstance ( file_or_name , basestring ) : fh = open ( file_or_name , ( ( append and 'a' ) or 'w' ) ) else : fh = file_or_name if append : fh . seek ( 0 , 2 ) writer = ( csv_writer or csv . writer ( fh , delimiter = ',' , quotechar = '"' , quoting = csv . QUOTE_MINIMAL ) ) if include_header : header = [ ] for ( idx , node ) in enumerate ( query . _select ) : if node . _alias : header . append ( node . _alias ) elif isinstance ( node , ( Field , Func ) ) : header . append ( node . name ) else : header . append ( ( 'col_%s' % idx ) ) writer . writerow ( header ) for row in query . tuples ( ) . iterator ( ) : writer . writerow ( row ) if close_file : fh . close ( ) return fh
create a csv dump of a query .