Select to view content in your preferred language

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

5981
10
Jump to solution
08-25-2014 02:51 PM
BenColeman1
New Contributor II

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?

0 Kudos
10 Replies
RichardFairhurst
MVP Honored Contributor

IN case you ever need to update SDE here is some code I am using to do that kind of editing.  I don't want to risk editing all 120k records at once, since SDE is touchy about bulk updates, so I have made the updateFC into a layer with a definition query to process fewer records.  The code below also illustrates that the field lists can be different between the source and the update feature class without major modifications to my code set up.  I am processing my records right now and it is updating my target FC extremely fast.

import arcpy

import os

sourceFC = r"L:\rfairhur\Layers\Local_Government\Local_Government.gdb\ReferenceData\RoadCenterline"

updateFC = r"C:\Users\RFAIRHUR\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\Trans Connection to SQL Server.sde\GDB_TRANS.TRANS.TRANSPORTATION_MAINT\CENTERLINE"

arcpy.MakeFeatureLayer_management(updateFC, "CENTERLINE", "CL_ID > 10000 AND CL_ID < 20001")

updateFC = "CENTERLINE"

workspace = r"C:\Users\RFAIRHUR\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\Trans Connection to SQL Server.sde"

edit = arcpy.da.Editor(workspace) 

edit.startEditing(False, True)  

edit.startOperation()

sourceFieldsList = ["CL_ID", "ROUTE_ORIENTED", "ONEWAYDIR", "CALTRANS_FUNCTIONAL_CLASS", "FEDROUTE", "FEDRTETYPE", "STROUTE", "STRTETYPE", "CTYROUTE", "MUNILEFT", "MUNIRIGHT", "ZIPLEFT", "ZIPRIGHT"] 

 

updateFieldsList = ["CL_ID", "ROUTE_ORIENTED", "TRAVEL_DIRECTIONS", "CA_FUNCTIONAL_CLASS", "FEDERAL_ROUTE", "FEDERAL_ROUTE_TYPE", "STATE_ROUTE", "STATE_ROUTE_TYPE", "COUNTY_ROUTE", "CITY_LEFT", "CITY_RIGHT", "ZIP_LEFT", "ZIP_RIGHT"]

print "Reading Records"

 

valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)} 

print "Records are read"

 

with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows: 

    for updateRow in updateRows: 

        keyValue = updateRow[0] 

        if keyValue in valueDict: 

            for n in range (1,len(sourceFieldsList)):   

                updateRow = valueDict[keyValue][n-1] 

            updateRows.updateRow(updateRow) 

 

del valueDict  

edit.stopOperation() 

edit.stopEditing(True)