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.
-
1You can read "Shape@" in your feature class, and use geometry method to construct the bufferFelixIP– FelixIP2016年10月03日 20:08:41 +00:00Commented Oct 3, 2016 at 20:08
-
2Your expression has two placeholders, but you're only passing one in the format method. What is the other variable supposed to be?Evan– Evan2016年10月03日 20:25:16 +00:00Commented Oct 3, 2016 at 20:25
-
Good catch, it's supposed to be .format(fcfield, var_segment).Andy Fisher– Andy Fisher2016年10月03日 20:35:13 +00:00Commented Oct 3, 2016 at 20:35
-
@AndyFisher does that solve the problem?Midavalo– Midavalo ♦2016年10月03日 20:53:22 +00:00Commented Oct 3, 2016 at 20:53
-
@Midavalo, it did not.Andy Fisher– Andy Fisher2016年10月03日 20:56:09 +00:00Commented Oct 3, 2016 at 20:56
1 Answer 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.
Explore related questions
See similar questions with these tags.