Loop through records based on unique value and stopping once max value is met

439
3
08-14-2019 12:42 PM
LuisResh
New Contributor II

I am trying to loop through records of a feature class with same site number ID's, perform a set of arcpy functions, and stop loop and continue on with later code once max ID is met. 

I used a cursor to find and set a max value variable of largest number from feature class Site ID field and then a for loop to iterate over the feature class to select records with the same site ID, perform acouple of functions and then stop once max value is met. Problem is the code keeps looping/creating FC's past the max value (which is 37 in the attribute table). Any advice?

Code Below:

arcpy.CreateFeatureDataset_management(Output_Location,"MapGrids",spatref)
max_value = -999999
with arcpy.da.SearchCursor("Merged_Assets", "Site_Num") as cursor:
    for row in cursor:
        max_value = max(int(row[0]), max_value)
 
count = 1
if count <= (max_value):   
    for row in cursor:
      arcpy.SelectLayerByAttribute_management("Merged_Assets","NEW_SELECTION",'"Site_Num" = \'{0}\''.format(count))
      arcpy.GridIndexFeatures_cartography(Output_Location + "\MapGrids" + "\Site" + str(count),"Merged_Assets","INTERSECTFEATURE","USEPAGEUNIT")
        arcpy.AddField_management(Output_Location + "\MapGrids" + "\Site" + str(count),"Site_Num","TEXT")
        countexp = count
        arcpy.CalculateField_management(Output_Location + "\MapGrids" + "\Site" + str(count),"Site_Num","'" + str(countexp) + "'","PYTHON_9.3")
        count = count + 1
Tags (3)
0 Kudos
3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

What is the purpose of the cursor on line 9?

LuisResh
New Contributor II

Removing Line 9 and combining the code worked. Still learning the basics of python.  

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

glad it worked out

0 Kudos