Select to view content in your preferred language

Search and Update Cursor Need help.

1819
10
Jump to solution
12-09-2013 06:21 AM
HectorChapa
Frequent Contributor
I am trying to update a shapefile by using search and update cursor.
It searchs great on of the shapefile but when I try to update the other it only retrieves the first data.
I am providing my code so anyone could tell me how to modify it.


import arcpy  fc = "C:/Users/hchapa.LRGVDC911.000/Documents/ArcGIS/Default.gdb/Test_Shapefile_SpatialJoin" field = "parcel_1"   fc2 = "C:/GIS_DATA/Test_Shapefile.shp"   field2 = "parcel" value = [] cursor = arcpy.SearchCursor(fc) UpdateCursor = arcpy.UpdateCursor(fc2)    for row in cursor:     value = row.getValue(field)     for row2 in UpdateCursor:         row2.setValue(field2, value)         UpdateCursor.updateRow(row2)        print "Done"




the problem it only gets the first number but it doesnt iterate to the second number.

What am I doing wrong.

P.S I already try many other solutions nothing works.

I tried using a list.
Tags (2)
0 Kudos
10 Replies
JoshuaChisholm
Frequent Contributor
Hello again Hector,

I think you're best switching to Wayne's code and adding a ".contains" line.

Wayne's Code (modified):
import arcpy

fc = "C:/Users/hchapa.LRGVDC911.000/Documents/ArcGIS/Default.gdb/Test_Shapefile_SpatialJoin"
field = "parcel_1"

fc2 = "C:/GIS_DATA/Test_Shapefile.shp"
field2 = "parcel"

value = [] #You should remove this line. It's not needed.

cursor = arcpy.SearchCursor(fc)
UpdateCursor = arcpy.UpdateCursor(fc2)

# fetch the 1st row, for both search/update cursors
row = cursor.next()
row2 = UpdateCursor.next()

while row:
    if row.shape.touches(row2.shape): 
        row2.setValue(field2, row.getValue(field))
        UpdateCursor.updateRow(row2)
    # advance both cursors to next row obj
    row = cursor.next()
    row2 = UpdateCursor.next()
    
    
del cursor, UpdateCursor

print "Done"



Although, a different process like Spatial Join may be a better approach for what you're doing.
0 Kudos