I am running my script in ArcGIS 10.2 for Desktop and it doesn't stop processing. The script is overwriting itself but I'm not sure how to stop it as its not the intended purpose? Help, my loop is looping crazy?;
with arcpy.da.SearchCursor(mLanduse_SpatialJoin, lField) as cursor: for row in cursor: with arcpy.da.UpdateCursor(mSoils, sField) as cursor2: for row2 in cursor2: if row2[0] == ' ': row2[0] = row[0] cursor2.updateRow(row2)
Why do you have a loop in a loop?
I don't know why, I was after;
row2[0] == ' ':
row2[0] = row[0]
So if a field is blank, replace it with field value from SearchCursor. Can I separate these loops to achieve the result?
I think you want something more like this (excuse any bad formatting):
arcpy.SpatialJoin_analysis(mLanduse, mSoils, mLanduse_SpatialJoin, "JOIN_ONE_TO_ONE", "Keep_All", "","WITHIN")
rows = arcpy.UpdateCursor(mLanduse_SpatialJoin)
for row in rows:
if row.getValue(sField) == " ":
row.setValue(sField, lField)
rows.updateRow(row)
del rows
Or maybe this:
rows = arcpy.UpdateCursor(InTable)
for row in rows:
if row.getValue(sField) == " ":
row.sField = row.lfield
rows.updateRow(row)
del row, rows
Sephe, arcpy.da cursors are preferred to the legacy arcpy cursors.
ArcGIS 10.1 added a new data access module (arcpy.da). The previously existing cursors (that are still listed under arcpy) are still functional and valid; however, the new arcpy.da cursors include significantly faster performance. In most cases, the help will illustrate the use of the arcpy.da cursors.
Blank, null or empty? These may mean different things with the da.SearchCursor and you will have to account for each. Also, you have extra space between your single quotes ' ', this should not have any spaces. Here's something to try (I have no idea if your update cursor within a search cursor is correct or a well formed approach, so this is not really tested as such):
with arcpy.da.SearchCursor(mLanduse_SpatialJoin, lField) as cursor: for row in cursor: with arcpy.da.UpdateCursor(mSoils, sField) as cursor2: for row2 in cursor2: #find empty/blank values if row2[0] == '': cursor2.updateRow(row2) #find null values elif row2[0] == None: cursor2.updateRow(row2) #otherwise just use search cursor row value else: row2[0] = row[0]
I believe you've got your update and search cursors mixed up. The way it is now, you are calling updateRow not once per empty cell in mSoils, but once per empty cell in mSoils times the number of rows in mLanduse_SpatialJoin. As a side effect, all empty cells in mSoils will get the last value in mLanduse_SpatialJoin. Do you want all of the blank cells to be the same value? If not, when you loop through the search cursor (as the inside loop) you need to evaluate the equivalency of some value in the update cursor to some matching value in the search cursor.