Quick export CSV w/ headers via python

4451
4
Jump to solution
06-22-2012 11:17 AM
gregmohler
New Contributor II
Hello,

I'm looking to export a large feature class (55k+ records) to csv with headers.  Can anyone help with how to set the parameters in python?
Using Arcmap and looking in the results windows after setting parameters gives:

CSV,S:\\Folder,"RUNTIME_MACROS,""APPEND,No,FIELD_NAMES,yes,SEPARATOR,"""","""",EXTENSION,csv"",META_MACROS,""DestAPPEND,No,DestFIELD_NAMES,yes,DestSEPARATOR,"""","""",DestEXTENSION,csv"",METAFILE,CSV,COORDSYS,,__FME_DATASET_IS_SOURCE__,false"

This gives me what i need, but I would rather not open Arcmap.  I've tried setting these items above a few ways to no avail.
I just need the input, output, and headers to be written.  From python with just input and output gives me a csv with no headers.

Python 2.6 with arc10.0.

Thanks for any help.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor
I don't know about Quick Export, but this exports a feature class to csv, with headers.
import arcpy, csv fc = "H:/GIS_Data/TEMP.gdb/points" #input feature class rows = arcpy.SearchCursor(fc) csvFile = csv.writer(open("H:/GIS_Data/csvfile.csv", 'wb')) #output csv fieldnames = [f.name for f in arcpy.ListFields(fc)]  allRows = [] for row in rows:     rowlist = []     for field in fieldnames:         rowlist.append(row.getValue(field))     allRows.append(rowlist)      csvFile.writerow(fieldnames) for row in allRows:     csvFile.writerow(row)

*mostly stolen from here

View solution in original post

0 Kudos
4 Replies
JasonScheirer
Occasional Contributor III
Right click on the result in the results window and copy as Python snippet.
0 Kudos
gregmohler
New Contributor II
That would be what i posted above.
Is there a special way to copy as python snippet?
I tried this
arcpy.QuickExport_interop('S:\Tech\Greg_Mohler\pythonwork\gm_sw_py.gdb\SW_LINE CSV','S:\Tech\Greg_Mohler\pythonwork',"RUNTIME_MACROS,""APPEND,No,FIELD_NAMES,yes,SEPARATOR,"""","""",EXTENSION,csv"",META_MACROS,""DestAPPEND,No,DestFIELD_NAMES,yes,DestSEPARATOR,"""","""",DestEXTENSION,csv"",METAFILE,CSV,COORDSYS,,__FME_DATASET_IS_SOURCE__,false")

and I got this:
TypeError: QuickExport() takes at most 2 arguments (3 given)
0 Kudos
DarrenWiens2
MVP Honored Contributor
I don't know about Quick Export, but this exports a feature class to csv, with headers.
import arcpy, csv fc = "H:/GIS_Data/TEMP.gdb/points" #input feature class rows = arcpy.SearchCursor(fc) csvFile = csv.writer(open("H:/GIS_Data/csvfile.csv", 'wb')) #output csv fieldnames = [f.name for f in arcpy.ListFields(fc)]  allRows = [] for row in rows:     rowlist = []     for field in fieldnames:         rowlist.append(row.getValue(field))     allRows.append(rowlist)      csvFile.writerow(fieldnames) for row in allRows:     csvFile.writerow(row)

*mostly stolen from here
0 Kudos
gregmohler
New Contributor II
I don't know about Quick Export, but this exports a feature class to csv, with headers.
import arcpy, csv
fc = "H:/GIS_Data/TEMP.gdb/points" #input feature class
rows = arcpy.SearchCursor(fc)
csvFile = csv.writer(open("H:/GIS_Data/csvfile.csv", 'wb')) #output csv
fieldnames = [f.name for f in arcpy.ListFields(fc)]

allRows = []
for row in rows:
    rowlist = []
    for field in fieldnames:
        rowlist.append(row.getValue(field))
    allRows.append(rowlist)
    
csvFile.writerow(fieldnames)
for row in allRows:
    csvFile.writerow(row)

*mostly stolen from here


Thanks
This works for the most part.  Someone used degrees symbols and other odd characters and i'm having encoding/decoding issues.
0 Kudos