Tell me more ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm not sure how my expression is failing. Any ideas?

inPoints = "points.shp"
i = 0
arcpy.MakeFeatureLayer_management(inPoints, "pts")
arcpy.SelectLayerByAttribute_management("pts", "NEW_SELECTION", '\"FID\" = i')
share|improve this question
is your file path correct? do you have any errors telling you which line is failing? – Craig Apr 22 at 18:47
The MakeFeatureLayer works fine. So the file path is right. I just get: File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 6435, in SelectLayerByAttribute raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute) – angelspatial Apr 22 at 18:51
Same problem: line 17, in <module> arcpy.SelectLayerByAttribute_management ("pts", "NEW_SELECTION", "[FID] = i") File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 6435, in SelectLayerByAttribute raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute). – angelspatial Apr 22 at 19:04

1 Answer

You need to use string formatting when inserting your counter, i, into the SQL query:

'\"FID\" = {}'.format(i)

As it stands now, your script is asking Arc to find an object with FID = i, and that does not exist.

share|improve this answer
2  
+1. As an aside - searching for features based on an FID is bad practice as they may be missing or reassigned in some cases. – Radar Apr 22 at 19:07
@Radar, I would agree with that. – Jason Apr 22 at 19:14
1  
Jason's answer does not work with Python 2.6.x. To use the string formatting in for ArcGIS 10.0 and 10.1 use the following: '\"FID\" = {0}'.format(i). – Saleika Apr 22 at 19:51
@Saleika, in the case where there is only one item in the "queue", the index number may be omitted: >>> print '\"FID\" = {}'.format(1) "FID" = 1 >>> print '\"FID\" = {:0>2d}'.format(1) "FID" = 01 See the string formatting guide I've linked in my answer above. If this is not the case in Python 2.6.x, then make sure to specify the index number inside the brackets, {}. However, it appears that @angelspatial is using Arc 10.1 which would indicate Python 2.7. – Jason Apr 22 at 19:52
Great! @Jason, The reason I was using FID was because it was recommended in response to another question I asked. link Do you have a better recommendation on how to handle that one? Feel free to answer it there. :) – angelspatial Apr 22 at 20:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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