I am writing a function to delete rows based on a certain value. The first code block below doesn't work; the second code block does work. I don't understand why I have to add the "if" clause to get it to work. Isn't the point of having a where clause (my sql variable) to select only the fields and rows that meet that condition?
Doesn't work, curious why?
def delRows(shp_in, fieldname, Val): """ Delete rows based on values in specific fields :shp_in: input shapefile for which rows are to be deleted :fieldname: fieldname for finding value to delete :Val: trigger value for row deletion """ import arcpy sql = "'" + fieldname + "' = " + "'" + str(Val) + "'" print sql with arcpy.da.UpdateCursor(shp_in, [fieldname], sql) as cursor: for row in cursor: cursor.deleteRow() #
Does work
def delRows(shp_in, fieldname, Val): """ Delete rows based on values in specific fields :shp_in: input shapefile for which rows are to be deleted :fieldname: fieldname for finding value to delete :Val: trigger value for row deletion """ import arcpy sql = "'" + fieldname + "' = " + "'" + str(Val) + "'" print sql with arcpy.da.UpdateCursor(shp_in, [fieldname], sql) as cursor: for row in cursor: if row[0] == Val: cursor.deleteRow() #
Edit Here's the print-out of the sql statement as coded in both of the above:
'Site' = '0' #
sqlstring?