Last active
October 9, 2017 10:25
-
-
Save JeffJacobson/06e154b3f27a2131640d to your computer and use it in GitHub Desktop.
ArcGIS conversion: Table View 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
"""Module for conversion from ArcGIS tables to CSV. | |
""" | |
import csv | |
import arcpy | |
import arcpy.da as da | |
def writeToCsv(table_view, output_file, field_names='*', where_clause=None, sql_clause=None): | |
"""Converts a table or table view into a CSV file. | |
""" | |
with open(output_file, 'wb') as csv_file: | |
csv_writer = csv.writer(csv_file, 'excel') | |
with da.SearchCursor(table_view, field_names, where_clause=where_clause, sql_clause=sql_clause) as cursor: | |
# Write the headers | |
csv_writer.writerow(cursor.fields) | |
for row in cursor: | |
csv_writer.writerow(row) |
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
import arcpy | |
import arcpy.da as da | |
import arcgiscsv | |
class Toolbox(object): | |
def __init__(self): | |
"""Define the toolbox (the name of the toolbox is the name of the | |
.pyt file).""" | |
self.label = "Export to CSV Toolbox" | |
self.alias = "csv" | |
# List of tool classes associated with this toolbox | |
self.tools = [ExportToCsv] | |
class ExportToCsv(object): | |
def __init__(self): | |
"""Define the tool (tool name is the name of the class).""" | |
self.label = "Export to CSV" | |
self.description = "Exports a table to CSV" | |
self.canRunInBackground = True | |
def getParameterInfo(self): | |
"""Define parameter definitions""" | |
table_param = arcpy.Parameter( | |
displayName = "Input Table", | |
name = "in_table", | |
datatype = "GPTableView", | |
parameterType = "Required", | |
direction = "Input") | |
out_csv_param = arcpy.Parameter( | |
displayName = "Output CSV File", | |
name = "out_csv_file", | |
datatype = "DEFile", | |
parameterType = "Required", | |
direction = "Output") | |
params = [table_param, out_csv_param] | |
return params | |
def isLicensed(self): | |
"""Set whether tool is licensed to execute.""" | |
return True | |
def updateParameters(self, parameters): | |
"""Modify the values and properties of parameters before internal | |
validation is performed. This method is called whenever a parameter | |
has been changed.""" | |
return | |
def updateMessages(self, parameters): | |
"""Modify the messages created by internal validation for each tool | |
parameter. This method is called after internal validation.""" | |
return | |
def execute(self, parameters, messages): | |
"""The source code of the tool.""" | |
table = parameters[0].valueAsText | |
csvFile = parameters[1].valueAsText | |
arcgiscsv.writeToCsv(table, csvFile) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment