0
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'

#variables
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?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Pfalbaum
  • 911
  • 6
  • 28

1 Answers1

3

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)‍‍‍‍‍‍‍‍‍‍‍
artwork21
  • 35,114
  • 8
  • 66
  • 134
  • 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 02:13