I cannot be the only person to come across this problem, but I have yet to find a solution that works.
I have a script that counts the number of features that intersects another feature, and cycles through the file using the UpdateCursor. Everything has been going smoothly, until I came across an attribute that has special characters in it. Due to reasons, I cannot remove the apostrophes, ampersands, forward slashes, etc. from the attribute data and have to account for these in the code.
edit.startEditing(False, True)
edit.startOperation()
with arcpy.da.UpdateCursor(subdivisions, ['Subd_Name', 'LOTS', 'CON_STATUS']) as subdivUpdate:
for each in subdivUpdate:
if subdivUpdate[2] != "UNDERCON" or subdivUpdate[2] != "ESTBLSHD":
sub = subdivUpdate[0]
query1 = 'Subd_Name = \'{}\''.format(sub)
subDiv = arcpy.management.SelectLayerByAttribute(subdivisions, 'NEW_SELECTION', query1)
selectedParcels = arcpy.management.SelectLayerByLocation(parcels, 'HAVE_THEIR_CENTER_IN', subDiv)
numberOfParcels = arcpy.GetCount_management(selectedParcels)
print(subdivUpdate[0])
each[1] = int(str(numberOfParcels))
print(numberOfParcels)
subdivUpdate.updateRow(each)
edit.stopOperation()
edit.stopEditing(True)
Everything runs the way it is supposed to until it comes across a record with a special character in it, and throws the error, arcgisscripting.ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).
I have tried several combinations of triple quotes around the query, but they've all failed as soon as it reaches a record with special characters. If there's a better way of dynamically inserting the query value I'm all ears.
Thanks in advance for any help!