arcpy.TableSelect_analysis(in_table, out_table, {where_clause})‍

"Selects table records matching a Structured Query Language (SQL) expression and writes them to an output table."

Using the arcpy.TableSelect_analysis() function I have this simple script:

import arcpy
from arcpy import env

env.workspace = r'\\gisfile\GISstaff\Jared\ModelBuilder\JaredTest.gdb'

intable = r"\\gisfile\GISstaff\Jared\ModelBuilder\JaredTest.gdb\ElectionResults_Nov2016"
outtable = r"\\gisfile\GISstaff\Jared\ModelBuilder\JaredTest.gdb\Results3"
where_clause = "ContestTitle = 'BALLOTS CAST - TOTAL'"

arcpy.TableSelect_analysis(intable, outtable, where_clause)‍‍‍‍‍‍‍‍‍‍‍

It selects all records named 'BALLOTS CAST - TOTAL' from the 'ContestTitle' column of my 'ElectionResults_Nov2016' table and writes them in the newly created 'Results3' table. 'BALLOTS CAST - TOTAL' is only one of 58 separate table record names.

How can I write all 58 to their own tables?


You can get all the unique values witin the field using a cursor like:

Trying to extract a list of Unique Values from a field using python

Then loop through the unique value list:

unique_values = set(row[0] for row in arcpy.da.SearchCursor(intable, "ContestTitle"))
# then loop through all unique values
for value in unique_values:
    where_clause = "ContestTitle = '{}'".format(value)
    # you may need to manipulate the value variable here to remove any
    # special characters or spaces that may cause issues in the fc table name
    outtable = r"\\gisfile\GISstaff\Jared\ModelBuilder\JaredTest.gdb\{}".format(value)
    arcpy.TableSelect_analysis(intable, outtable, where_clause)‍‍‍‍‍‍‍‍‍‍‍
  • That worked great up to a point. It wrote four tables, but then threw an error: ExecuteError: ERROR 000210: Cannot create output \\gisfile\GISstaff\Jared\ModelBuilder\TEST folder\TEST.gdb\COUNTY BOARD MEMBER DISTRICT FOUR (note: I changed the output location) It may be, as you said, because of the value variable. I'll look into it. But, overall it did as I wanted. Thanks! – Pfalbaum Jan 19 '18 at 21:00
  • Table names cannot have spaces, use something like value.replace(' ','') to replace blank space with underscore "" character. – artwork21 Jan 20 '18 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.