Using field values to update a field using a search and update cursor

Discussion created by gardta on Apr 12, 2017
Latest reply on Apr 12, 2017 by gardta

I am trying to update a field using the arcpy.da.Update cursor where I can use two field values from a select by location result to populate a new field. It seems like a spatial join route may be the best approach here. With this code, I am now receiving an error of 'tuple' object has no attribute 'GEN_UNITS' for the 'result = joinsubrow.GEN_UNITS/joinsubrow.LINES' line. Do I need to search out these field values separately in separate search cursors? What would be the best route here? Thanks in advance!


   import arcpy    
from arcpy import env     
.env.OverwriteOutput = True    
= 'C:\Topo_Check_Tess_V5.gdb'    
.AddField_management(subLayer, "GU_Count", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")   
GUCountField = "GU_Count"
#Make feature layer   
.MakeFeatureLayer_management(ppLayer, "ppLayerGU")    

#Make feature layer   
.MakeFeatureLayer_management(transLayer, "transLayerGU")    

SubLayer_pp_trans_GUJoin ='C:\Topo_Check_Tess_V5.gdb\SubLayer_pp_trans_GUSpatialJoin'    

#spatial join of feature layers   
.SpatialJoin_analysis ("transLayerGU", "ppLayerGU", pp_trans_GUJoin,'JOIN_ONE_TO_ONE', 'KEEP_ALL', '#', 'WITHIN_A_DISTANCE', .000002)      

#Make feature layer   
.MakeFeatureLayer_management(subLayer, "SubLayer_Layer")    

#spatial join of feature layers   
.SpatialJoin_analysis ("SubLayer_Layer", "ppLayerGU", SubLayer_pp_trans_GUJoin, 'JOIN_ONE_TO_ONE', 'KEEP_ALL', '#','WITHIN_A_DISTANCE', .000002)
#Make feature layer   
.MakeFeatureLayer_management(SubLayer_pp_trans_GUJoin, "JoinLayer")    

= ['LINES','GEN_UNITS']     

with arcpy.da.UpdateCursor (subLayer, [GUCountField, "SHAPE@"]) as GUCalcCursor:       
for subrow in GUCalcCursor:           
.SelectLayerByLocation_management("JoinLayer",'WITHIN_A_DISTANCE', subrow[1], .000002, "NEW_SELECTION")              
with arcpy.da.SearchCursor("JoinLayer", LINE_Fields) as JoinLayerCursor:               
for joinsubrow in JoinLayerCursor:                    
= joinsubrow.GEN_UNITS/joinsubrow.LINES          
[0] = result