3

I'm currently trying to iterate through the features of a feature class (using an update cursor, because the goal is to ultimately update the field) and buffer each individual segment. I eventually want to use the buffer in each iteration with different tools, but I can't get the initial Select by Attribute to work. Here's my code:

fc = "Original_Clip"
fcfield = "SEGMENTID"

strt_lyr = arcpy.MakeFeatureLayer_management(fc, "Street_lyr")
with arcpy.da.UpdateCursor(fc, fcfield) as cursor:
     for row in cursor:
        var_segment = row[0]
        print "Buffering segment " + str(var_segment)
        arcpy.SelectLayerByAttribute_management(strt_lyr, "NEW_SELECTION", '{} = \'{}\''.format(var_segment))
        arcpy.Buffer_analysis(strt_lyr, str(var_segment) + "Buff", "20 Feet") 

When I run this code, an error is thrown with the SelectLayerByAttributefunction, saying my where clause is invalid?

Runtime error Traceback (most recent call last): File "", line 15, in File "d:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute). I'm assuming I just have the SQL syntax wrong (I'm querying a file geodatabase btw), but maybe what I'm trying to do just isn't possible.

  • 1
    You can read "Shape@" in your feature class, and use geometry method to construct the buffer – FelixIP Oct 3 '16 at 20:08
  • 2
    Your expression has two placeholders, but you're only passing one in the format method. What is the other variable supposed to be? – Evan Oct 3 '16 at 20:25
  • Good catch, it's supposed to be .format(fcfield, var_segment). – Andy Fisher Oct 3 '16 at 20:35
  • @AndyFisher does that solve the problem? – Midavalo Oct 3 '16 at 20:53
  • @Midavalo, it did not. – Andy Fisher Oct 3 '16 at 20:56
1

I've had problems like this with where clauses in SelectLayerByAttribute when I try to build the clause right in the function parameter list. I work around it by building the where clause in a string variable and using the variable in the function parameters instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.