1

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.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Apr 7, 2019 at 18:45
5
  • 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. Commented 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! Commented Apr 7, 2019 at 20:43
  • Click the tag button for ERROR 000358 to see how others have researched/solved this. Commented Apr 7, 2019 at 21:07
  • @PolyGeo thanks for your suggestions. I find a similar question and post my solution. Commented Apr 7, 2019 at 21:28
  • Which question did you find? This Q&A should almost certainly be a duplicate of that one. Commented Apr 7, 2019 at 21:56

1 Answer 1

0

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Apr 7, 2019 at 21:27
1
  • 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 string format() method is inserting the actual value into the string Commented Apr 9, 2019 at 22:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.