I am trying to select by an attribute with a condition that field equals to an element of a vector. I use a for loop but could not get the right expression for this condition. Here are my codes.
random37 = [30449, 46617, 17309]
pointlayer = "network_nodeSelected"
for random in random37:
randomname = "n" + str(random)
arcpy.MakeFeatureLayer_management(pointlayer,pointlayer)
arcpy.SelectLayerByAttribute_management(pointlayer,'NEW_SELECTION','"ARC_" = random')
arcpy.CopyFeatures_management(pointlayer, randomname)
print randomname
The error message is presented as follows.
Traceback (most recent call last):
File "", line 6, in arcpy.SelectLayerByAttribute_management(pointlayer,'NEW_SELECTION','"ARC_" = point')
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\management.py", line 7742, in SelectLayerByAttribute raise e
ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).
As you can see, my problem is the expression '"ARC_" = random'. But I do not know how to get the right expression.
-
What happens when you run the code that you have presented? I suspect the error message will lead you to an answer in an existing Q&A here. Also, your three functions can be replaced by Select_analysis() for simpler code.PolyGeo– PolyGeo ♦2019年04月07日 19:52:14 +00:00Commented Apr 7, 2019 at 19:52
-
@PolyGeo, thanks for helping. I have added the error message. The error is the invalid expression and failed to execute SelectLayerByAttribute. Could you help me find the right expression? Really appreciate!Zhenyu– Zhenyu2019年04月07日 20:43:30 +00:00Commented Apr 7, 2019 at 20:43
-
Click the tag button for ERROR 000358 to see how others have researched/solved this.PolyGeo– PolyGeo ♦2019年04月07日 21:07:41 +00:00Commented Apr 7, 2019 at 21:07
-
@PolyGeo thanks for your suggestions. I find a similar question and post my solution.Zhenyu– Zhenyu2019年04月07日 21:28:55 +00:00Commented Apr 7, 2019 at 21:28
-
Which question did you find? This Q&A should almost certainly be a duplicate of that one.PolyGeo– PolyGeo ♦2019年04月07日 21:56:39 +00:00Commented Apr 7, 2019 at 21:56
1 Answer 1
The problem is the sql expression. Here is the solution.
random37 = [30449,46617,17309]
pointlayer = "network_nodeSelected"
layer1 = "wqi_ConProshp"
for point in random37:
pointname = "n" + str(point)
fieldname = 'ARC_'
whereClause = """{0} = {1}""".format(arcpy.AddFieldDelimiters(pointlayer, fieldname),point)
arcpy.MakeFeatureLayer_management(pointlayer,pointlayer)
arcpy.SelectLayerByAttribute_management(pointlayer,'NEW_SELECTION',whereClause)
arcpy.CopyFeatures_management(pointlayer, pointname)
#select the location and then export it
print pointname
To be honest, I still can not understand how it works but it indeed solves my problem.
-
In your original post, the where clause literally had the word "random" in the string as opposed to the value of the variable called
random
. In this example, the stringformat()
method is inserting the actual value into the stringmikewatt– mikewatt2019年04月09日 22:50:19 +00:00Commented Apr 9, 2019 at 22:50
Explore related questions
See similar questions with these tags.