Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 '13 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) – Spatial Pariah Apr 22 '13 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). – Spatial Pariah Apr 22 '13 at 19:04
    
possible duplicate of Errors using SelectLayerByAttribute in ArcPy – PolyGeo Dec 12 '13 at 7:40
    
shapefile or feature class? i believe the syntax changes depending on the type – ziggy May 18 at 19:40

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 '13 at 19:07
    
@Radar, I would agree with that. – Jason Apr 22 '13 at 19:14
2  
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 '13 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 '13 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. :) – Spatial Pariah Apr 22 '13 at 20:15

Could be helpful for future:

arcpy.SelectLayerByAttribute_management("pts", "NEW_SELECTION", "\"FID\" = "+ str(i))

In this example, value is int, but expression has to be str, thus just convert it str(i)and append it to expression. In addition, if the value is str i.e. i='0', then I would apply format, i.e. "\"FID\" = \"{}\" ".format(i)

share|improve this answer
    
Can you please expand on your answer to explain why it could be helpful – Midavalo May 18 at 19:28

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.