More

Arcpy search cursor not working


Im running my script in ArcGIS 10.2 and the dialog window says completed, but the empty fields in the 'SoilsType' row of the 'mSoils' Feature Class aren't getting populated with the values from the 'SoilsType' field from the 'mLanduse' Feature Class.

cur = arcpy.da.SearchCursor(mLanduse, lField) for row in cur: cur2 = arcpy.da.UpdateCursor(mSoils, sField) for row2 in cur2: if row2[0] =="or row2[0] is None: row2[0] = lField cur2.updateRow(row2)

Instead of going the python route, I would recommend using this sequence of geoprocessing tools:

  1. Create new field in soil table called layer, and field calculate it with the value "soil"
  2. Perform union on soil layer and land use (soil first input and land use second input)

The union process will help you deal with soil features that span multiple land use features. This will bring over the other soil type value/field from the land use table into one table (soil_landuse_union)

  1. Finally, select records in soil_landuse_union that have a value of "soil" in the layer field that do not have a soil type and populate the soil type field with the unioned land use soil type field/value

If you have to perform this operation frequently, you could create a model or python script using the geoprocessing tools/steps above.


Change your exception handler to this:

# Return any errors except Exception as e: print(e) arcpy.AddMessage(arcpy.GetMessages())

Now open py file in IDLE and try to run. See what error you have.

ED|T: for row3 in row2: = you'll get a "row2 is undefined" error here in case you'll manage to get here :) First clean up syntax errors first and people here can help you to solve the issue.


One of the primary problems is in the following line of code:

rows3 = mLanduse

Here you are trying to pass a feature class (mLanduse) into the attribute table, which cannot be done. Instead, you need pass a integer, float, string etc. Perhaps you meant to do the following:

rows3[0] = row[0]

You're making a big assumption with your SearchCursor/UpdateCursor pairing that the features from both match up exactly in order. That might be 'okay' for your data now, but it might not always be.

If the features match up spatially, which seems likely based on your question, then just use the Spatial Join tool: http://desktop.arcgis.com/en/desktop/latest/tools/analysis-toolbox/spatial-join.htm


Watch the video: Arcpy Data Access Module Search Cursor (September 2021).