Table of Contents
The Video
Introduction
Learn how to export a feature class attribute table to CSV using ArcPy in ArcGIS Pro. We will cover both exporting all records and fields and creating a filtered CSV with selected fields and a SQL where clause.
ArcGIS Pro Definition Query Masterclass with ArcPy & Python
Free on YouTube! Witness the power of Definition Queries in ArcGIS Pro through this comprehensive course that dives managing and utilizing definition queries with spatial data using ArcPy and Python scripting. In this course, we will focus exclusively on Definition Queries, covering their creation, modification, and application through hands-on exercises and real-world scenarios.
Import ArcPy
arcpy module.
import arcpy
User Inputs
in_table which is the feature class or table to export to CSV. The second is the filepath to create the CSV file – out_csv.
## the Feature CLass or Table to export to CSV
in_table = r"path\to\feature_class\or\table"
## the fielpath for the CSV file to create
out_csv = r"path\to\output\file.csv"
Export all features to CSV
We can use the Export Table geoprocessing tool from the Conversion toolbox and use the two required parameters to export all features and attribute to a CSV file. Run the script to here if you want to export all data.
## Export Table geoprocessing tool with required parameters
arcpy.conversion.ExportTable(
in_table=in_table,
out_table=out_csv
)
Export a selection of fields and a subset of features
where_clause and use the field_mapping parameter with the Export Table tool to select a subset of fields to export.
## the list of fields to export
fields_list = [
"FIELD_NAME_1", "FIELD_NAME_2", "FIELD_NAME_3", "FIELD_NAME_N"
]
## create a FieldMappings object
field_mappings = arcpy.FieldMappings()
## for each field name
for field in fields_list:
## create a FieldMap object
field_map = arcpy.FieldMap()
## add Field to the FieldMap
field_map.addInputField(
in_table, # table_dataset
field # field_name
)
## add the FieldMap object to the FieldMappings
field_mappings.addFieldMap(
field_map # field_name
)
## Export Table with option field_mapping and where_clause
arcpy.conversion.ExportTable(
in_table=in_table,
out_table=out_csv,
where_clause="FIELD_NAME = 'Value'",
field_mapping=field_mappings
)
Mastering ArcGIS Pro ArcPy Search, Insert, & Update Cursors
Free on YouTube! ArcPy Cursors from the data access module enable you to streamline your GIS workflows, automate repetitive tasks, and witness the full potential of spatial data manipulation in ArcGIS Pro by accessing, inserting, and updating features in your datasets.
All the code in one place
import arcpy
########################################################################################
## Esri Documentation:
## https://pro.arcgis.com/en/pro-app/latest/tool-reference/conversion/export-table.htm
## https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/fieldmappings.htm
## https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/fieldmap.htm
##
########################################################################################
########################################################################################
## USER INPUT
## the Feature CLass or Table to export to CSV
in_table = r"path\to\feature_class\or\table"
## the fielpath for the CSV file to create
out_csv = r"path\to\output\file.csv"
########################################################################################
## Example 1: All Fields
## Export Table geoprocessing tool with required parameters
arcpy.conversion.ExportTable(
in_table=in_table,
out_table=out_csv
)
########################################################################################
## Example 2: Selection of fields & subset of records
## the list of fields to export
fields_list = [
"FIELD_NAME_1", "FIELD_NAME_2", "FIELD_NAME_3", "FIELD_NAME_N"
]
## create a FieldMappings object
field_mappings = arcpy.FieldMappings()
## for each field name
for field in fields_list:
## create a FieldMap object
field_map = arcpy.FieldMap()
## add Field to the FieldMap
field_map.addInputField(
in_table, # table_dataset
field # field_name
)
## add the FieldMap object to the FieldMappings
field_mappings.addFieldMap(
field_map # field_name
)
## Export Table with option field_mapping and where_clause
arcpy.conversion.ExportTable(
in_table=in_table,
out_table=out_csv,
where_clause="FIELD_NAME = 'Value'",
field_mapping=field_mappings
)
########################################################################################
print("\nSCRIPT COMPLETE")