Last active
August 29, 2015 14:19
-
-
Save cindygis/6c209466175e56df292d to your computer and use it in GitHub Desktop.
Writes the contents of a feature class to csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# @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