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.

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

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
    – mikewatt
    Commented Apr 9, 2019 at 22:50

Your Answer

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

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