I'm exporting a feature class as a CSV through Python with ArcGIS Pro. I've listed out all the fields I need to keep on export.
Is it possible to use this as a list for ordering my headers in the CSV?
There's a csv.DictWriter
but I can't figure out where/how to combine it. I thought listing it out preserves the order in export but it does not.
fields = arcpy.ListFields(fc3)
fieldsToKeep =['REPORTER','RECOWNER','SCINAME','COMNAME','RECBASIS','GlobalID','OCCSTATUS','MANAGESTAT',
'POPSTAT','OBSDATE','COUNTRY','STATE','COUNTY','COMMENTS']
field_names = [field.aliasName for field in fields if field.name in fieldsToKeep]
with open(outputCSV,'w', newline='') as f:
w = csv.writer(f)
#w = csv.DictWriter(f, field_names)
w.writerow(field_names)
for row in arcpy.SearchCursor(fc3):
field_vals = [row.getValue(field.name) for field in fields if field.name in fieldsToKeep]
w.writerow(field_vals)
del row