0

I'm working on a script that takes a few feature classes, unions them together and then runs a series of geoprocesses on them and puts it into a single feature class. From that feature class I have a for loop set up that is to use CopyFeatures_management to create new feature classes based on the value of a specific field (in this case: PIN) and name the new feature class "CAUV_"+PIN(from attribute table).

It runs through everything once and creates the new feature class as it should but then it stops. It won't iterate through the rest of them. What on earth did I miss?

#Iterate over values in attribute table
def unique_values(table, field):
    cursor = arcpy.da.SearchCursor(table, [field])
    unique_values = []
    for row in cursor:
        if not row[0] in unique_values:
            unique_values.append(row[0])
            return sorted(unique_values)
unique_pin = unique_values(table=ParcelsLandUse, field="ParcelPublishingAuditor_PIN")

#Select Features and Make Layer
ParcelsLandUse_lyr = arcpy.MakeFeatureLayer_management(ParcelsLandUse, "ParcelsLandUse_lyr")
with arcpy.da.SearchCursor(ParcelsLandUse_lyr, "*") as cursor:
    for PIN in unique_pin:
        expression = "%s = '%s'" % ("ParcelPublishingAuditor_PIN", PIN)
        arcpy.SelectLayerByAttribute_management(ParcelsLandUse_lyr, "NEW_SELECTION", expression)
        outCopy = "Y:\\Data\\AuditorGIS.gdb\\Calculations\\CAUV_"+PIN
        arcpy.CopyFeatures_management(ParcelsLandUse_lyr, outCopy)
        arcpy.SelectLayerByAttribute_management(ParcelsLandUse_lyr, "CLEAR_SELECTION")
        info = "Processed: " + outCopy
del unique_pin
del PIN
info = "CAUV Parcel Soils Complete"
arcpy.AddMessage(info)
2

The problem is that there is a single unique pin, because the function instructs to return after the first one. Look at the indentation below:

def unique_values(table, field):
    cursor = arcpy.da.SearchCursor(table, [field])
    unique_values = []
    for row in cursor:
        if not row[0] in unique_values:
            unique_values.append(row[0])
            #return sorted(unique_values)   #Don't exit the function, keep iterating!
    return sorted(unique_values) #now that all rows have been processed, you can sort and return
2
  • That fixed it! Thank you! – Jessica Goodsell Mar 24 at 10:35
  • @JessicaGoodsell great! Please consider accepting the answer (green check mark to the left of the question) to let others know there is a working solution – JGH Mar 24 at 11:28

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.