AnsweredAssumed Answered

Search Cursor (where clause), Insert Cursor - based on unique values

Question asked by BahamutZ on Jun 3, 2016
Latest reply on Jun 3, 2016 by blake.terhune

Seeking some guidance related to search cursor's where clause.  Currently, I am grabbing unique field values from a single column using the unique_values function on the field Name.  I am attempting to export tables by unique Name values, passing the fields I want to it along with the rows associated with that unique name.  So if there is three exported tables, there will be three unique Name values.  I can get the unique Name tables exported (with my fields), but the problem is the insert cursor is adding the last Name value (alphabetically)  and its rows into each of these tables.  So I end up with the same rows in each table, but the right names of tables.  


#not all the code, but the relevant stuff.
def unique_values(table, field):
    with arcpy.da.SearchCursor(table,[field]) as scursor:
        return sorted({row[0] for row in scursor})

uName = unique_values(workspace+outname, "Name")

for uNameSplit in uName:
     query = "Name = '{0}'".format(uNameSplit)
     for uNames in uName:
         uNames = dbas.replace(" ","")
         uNames = dbas.replace(",","")
         uNames = dbas.replace(".","")

     with arcpy.da.SearchCursor(workspace+outname, fields, query) as scursor:
           arcpy.CreateTable_management(workspace, outname+"_Name_"+uNames)
           with arcpy.da.InsertCursor(outname+"_Name_"+uNames, fields) as icursor:
               for row in scursor:


I thought the for loop would work to pass one query at a time,  as it does for the table filename variable, but it does not for the search and inset cursor portion.


I have tried passing in a list of values, but the where clause takes a string.  May be missing something obvious.  Thanks for any help!