Skip to content

Instantly share code, notes, and snippets.

@cindygis
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cindygis/6c209466175e56df292d to your computer and use it in GitHub Desktop.
Save cindygis/6c209466175e56df292d to your computer and use it in GitHub Desktop.
Writes the contents of a feature class to csv
#
# @date 16/04/2015
# @author Cindy Williams
#
# Writes the contents of a feature class to csv.
# Writing to csv is usually used when quickly
# needing to give file contents to a non-GIS
# person, or in creating a GIS inventory.
#
# For use as a standalone script.
#
import arcpy
import csv
arcpy.env.workspace = r"C:\Some\Arb\Folder"
csv_file = r"output.csv"
# Always create feature layer for faster access
ftr = arcpy.management.MakeFeatureLayer("work.gdb\ftr")
# Exclude all shape fields from output
fields = [field.name for field in arcpy.ListFields(ftr) if not field.name.startswith("Shape")]
with open(csv_file, 'wb') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
with arcpy.da.SearchCursor(ftr, fields) as cursor:
for row in cursor:
csvwriter.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment