AnsweredAssumed Answered

Nested for loops not iterating correctly with arcpy.da.SearchCursor

Question asked by jbfcoleman on Aug 25, 2014
Latest reply on Aug 28, 2014 by jbfcoleman

I am attempting to update a feature class with records from a table.  I'm doing this by listing all fields in the table, creating arcpy.da.SearchCursor objects from that list, then iterating through the rows of both the table and feature class, find when the ID fields match, then update the fields in the feature class row with values from the table row which share the same field name.  However, I have discovered that the code (below) is only iterating through one value of the outermost for loop.  I have only included the portion of code that is causing a problem.  As you can see, the code only shows the mechanics of the loop.

 

import arcpy

arcpy.env.workspace = "path to file geodatabase"

points = "point_fc" #point feature class in geodatabase
table = "table" #table in geodatabase

fields = [f.name for f in sorted(arcpy.ListFields(table))]

id_index = fields.index('ID')

pcursor = arcpy.da.SearchCursor(points,fields)
tcursor = arcpy.da.SearchCursor(table,fields)

for prow in pcursor:
     for trow in tcursor:
          print prow[id_index],trow[id_index]


 

When I run this code, it iterates through all ID values of trow (from the table), but only one of prow (from the point feature class), then stops.  Any suggestions?

Outcomes