jay.kapalczynski

Add header to csv file

Discussion created by jay.kapalczynski on Nov 22, 2016
Latest reply on Nov 22, 2016 by jay.kapalczynski

I am creating a csv file via the SearchCursor.  This Works great....although I am not getting the field names in there.  I went about this 2 ways.  I have tried a ton of forums but just cant seem to get the right syntax to create this ONLY once and when I bring it into Excel it has a header for each field.

 

Any thoughts?

 

I add the headers IN the "for row in cursor"

This adds them BUT it creates a set of headers for each record imported

with arcpy.da.SearchCursor(fc, [var_region_1 ,var_county_1,var_ramp_1], where_clause=expression) as cursor:
   for row in cursor:
       zval = str('{0},{1},{2}'.format(row[0],row[1],row[2]))
       headers = ['region_1','county_1','ramp_1']
       outFile.write('\t'.join(headers) + '\n')       
       outFile.write(zval + "%s\n")

 

 

Add headers BEFORE the "for row in cursor"

This adds them but when I bring into excel they are all crammed into the first box, not spread out for each field.

headers = ['region_1','county_1','ramp_1']
outFile.write('\t'.join(headers) + '\n')

with arcpy.da.SearchCursor(fc, [var_region_1 ,var_county_1,var_ramp_1], where_clause=expression) as cursor:
   for row in cursor:
      zval = str('{0},{1},{2}'.format(row[0],row[1],row[2]))
      outFile.write(zval + "%s\n")

Outcomes