I've been working on this code for about 3 days and all of my time has been spent trying to understand why I can only execute the deleteRow() method on the first pair of cursors. The idea is that a cursor will iterate through the rows of a .dbf table and remove any rows whose attributes values in a dictionary created by the SearchCursor. Here is the applicable code:
with arcpy.da.SearchCursor(ParcelJoin, ["Name"]) as cursor:
lookupTbl = {}
for row in cursor:
lookupTbl.update({row[0]:row[0]})
with arcpy.da.UpdateCursor(Foreclosure, ["Parcel"]) as cursor:
for row in cursor:
if row[0] in lookupTbl:
cursor.deleteRow()
else:
continue
print('Joined features have been removed!')
with arcpy.da.SearchCursor(AddressJoin, ["FullAddr"]) as cursor:
for row in cursor:
lookupTbl.update({row[0]:row[0]})
with arcpy.da.UpdateCursor(Foreclosure, ["Address"]) as cursor:
for row in cursor:
if row[0] in lookupTbl:
cursor.deleteRow()
else:
continue
print('Joined features have been removed!')
Foreclosure is a .dbf of property sales, ParcelJoin is a join between a parcel layer and the Foreclosure table based on ParcelID and AddressJoin is the same feature class joined on address.
The first iteration of the cursor pairing executes successfully and removes the relevant rows. However, the second pairing iterates properly (I've checked by creating a counter to see how many times the code iterates) but does not execute the deleteRow(). I've looked all over the web but can't find any examples of code that use two cursor pairs in this way.
Finally figured it out, there were trailing whitespaces in the Foreclosure table that needed to be stripped. Code with list comprehension works beautifully!