1

I would like to update a field in a single feature class by a.) searching for a field where the values are not blank and b.) searching for a field where the values are blank.

A typical scenario of this update would occur if there are two service requests in one feature class with the same service request number with one being updated. When the updated feature is in the feature class, it does not maintain all of the same values that the previous feature class held. The one field that I would like to update is VehicleNam. So if I have a feature class where SR # 555 appears twice, as OID1 and OID2, but only OID1 has a value for VehicleNam I would like to update the VehicleNam field in OID2 as well. Here is the script that I have so far, however an error is being thrown at line 5. I am using ArcGIS 10.2.2

with arcpy.da.UpdateCursor(fc, ["VehicleNam"],"""VehicleNam <> ''""") as ucursor: with arcpy.da.SearchCursor(fc, ["VehicleNam"],"""VehicleNam = ''""") as scursor: for srow in scursor: svalue = srow[0] urow = ucursor.next() # instead of a full "for urow in ucursor" loop each time urow[0] = svalue ucursor.updateRow(urow)

Runtime error Traceback (most recent call last): File "<string>", line 5, in <module> StopIteration

2 Answers 2

2

Creating two cursors on the same feature class is going to be problematic. I'd create a dictionary of your matches first, and then iterate again and update.

Something like this (untested):

#SR field name
srField = "SR_FIELD_NAME"

vehNameDi = dict ([(sr, vehNam) for sr, vehName
                   in arcpy.da.SearchCursor
                   (fc, [srField, "VehicleNam"],"""VehicleNam <> ''""")])


with arcpy.da.UpdateCursor(fc, [srField, "VehicleNam"],"""VehicleNam = ''""") as cursor:
    for sr, vehNam in cursor:
        if not sr in vehNameDi:
            continue
        row = (sr, vehNameDi [sr])
        cursor.updateRow (row)

del cursor
4
  • I think this is a better approach too i.e. using a dictionary as a lookup table in place of a second cursor.
    – PolyGeo
    Oct 8, 2015 at 0:00
  • Emil, I am misunderstanding line 4, can you please clarify? Oct 8, 2015 at 0:00
  • @GeoffreyWest I think if da.SearchCursor should be in arcpy.da.SearchCursor
    – PolyGeo
    Oct 8, 2015 at 0:02
  • 1
    Whoops, yes arcpy.da.... I also had if where there should have been in. Edited. @Geoffrey look into list comprehension. Oct 8, 2015 at 0:18
0

Instead of:

urow = ucursor.next()

I think you should try:

urow = next(ucursor)

but also check the indentation on the last line of the code you presented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.