0

I'm working with ArcGIS 10.2. I used this code to select one record of an attribute table:

import arcpy
# Set the workspace
env.workspace = "F:ArcGIS\Trab\Model\Lin_150_tr.shp"
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("Lin_150_tr", "lyr5") 

arcpy.SelectLayerByAttribute_management("lyr5", "NEW_SELECTION", ' "FID" = 2 ')

And it works! Now I need to select no one record but some records of the same table, so I used a list of values and a loop:

import arcpy
env.workspace = "F:ArcGIS\Trab\Model\Lin_150_tr.shp"
arcpy.MakeFeatureLayer_management("Lin_150_tr", "lyr5")  

crit_150=[2,10,200,323,349,384,433]
for a in crit_150: 
    arcpy.SelectLayerByAttribute_management("lyr5", "NEW_SELECTION", ' "FID" = a ')

But it gives me an error:

Runtime error  Traceback (most recent call last):   File "<string>", line 21, in <module>   File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6494, in SelectLayerByAttribute     raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

How can I do this?

0

2 Answers 2

2

a is considered as part of the string ' "FID" = a '. The variable a shouldn't be part of the string expression. Instead you can use this notation for example, making use of the python string.format() function:

arcpy.SelectLayerByAttribute_management("lyr5", "NEW_SELECTION", """"FID" = {}""".format(a))

I doubt the field delimiters are necessary here, """FID = {}""".format(a) is probably fine as well.

EDIT

If you want to select to select all rows with values from your list at once, use this expression:

import arcpy
arcpy.MakeFeatureLayer_management(r"F:ArcGIS\Trab\Model\Lin_150_tr.shp", "lyr5")  

arcpy.SelectLayerByAttribute_management("lyr5", "NEW_SELECTION", """FID IN (2,10,200,323,349,384,433)""")
4
  • Now it runs, without error but it doesn't show the different records, shows only the first.. And I confirmed tis when I open the table "lyr5", where only appears one row..
    – S.Rai
    Commented Aug 29, 2016 at 11:25
  • What you're doing is iterating over the values in your crit_150 list, and selecting one record at a time. If you want to select all values at once you should modify the expression, I'll add it to my answer
    – GISGe
    Commented Aug 29, 2016 at 11:30
  • Yes, I need to select all this values at the same time
    – S.Rai
    Commented Aug 29, 2016 at 11:32
  • It continues to do just the first value and I need a way to run the list and show all values of this list..
    – S.Rai
    Commented Aug 29, 2016 at 11:48
0

Instead of hard coding the list of values (like @GISGe), I would build a string and pass it as variable:

import arcpy
arcpy.MakeFeatureLayer_managemen(r"F:\ArcGIS\Trab\Model\Lin_150_tr.shp", "lyr5")   

crit_150=[2,10,200,323,349,384,433]
crit_150_str = ','.join(str(val) for val in crit_150)
arcpy.SelectLayerByAttribute_management("lyr5", "NEW_SELECTION", '"FID" IN ({})'.format(crit_150_str))
2
  • 1
    Str(tuple(crit)) will do the job without join etc
    – FelixIP
    Commented Aug 29, 2016 at 19:42
  • @FelixIP: this only works if the list has more than one element. If not the output would be e.g.: "('2',)", which is no correct SQL.That's why I would prefer my solution, since it is more generic.
    – Saleika
    Commented Aug 31, 2016 at 5:58

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.