I am trying to use Python to select all records whose attribute (which is a number) in a certain field is within a list. I cannot get the syntax correct. my list is:
upstream = [9841978, 9842008, 9841966]
I have tried several expressions without success, including:
expression = '"COMID" in ' + str(tuple(upstream))
I am receiving the following error:
Runtime error Traceback (most recent call last): File "<string>", line 2, in <module>
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 7182, in SelectLayerByAttribute
raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).
arcpy.SelectLayerByAttribute_management(catchments, "NEW_SELECTION", expression)
1 Answer 1
Try removing the quote marks around your field name COMID
expression = 'COMID IN {0}'.format(tuple(upstream))
arcpy.SelectLayerByAttribute_management(catchments, "NEW_SELECTION", expression)
answered Mar 9, 2017 at 21:13
-
I am receiving the same error (see edits in original post) with this expression.mmoore– mmoore2017年03月09日 21:27:55 +00:00Commented Mar 9, 2017 at 21:27
-
1@mmoore can you confirm you removed the extra quote marks? That is the exact error I get if I include the quote marks around the field name, but the expression works if I remove the quote marks2017年03月09日 21:33:48 +00:00Commented Mar 9, 2017 at 21:33
-
I have removed them... this is the expression I used, copy and pasted straight from the code that I tried: expression = 'COMID IN {0}'.format(tuple(upstreams))mmoore– mmoore2017年03月09日 21:35:32 +00:00Commented Mar 9, 2017 at 21:35
-
1@mmoore Odd - I just used the same setup: feature layer from FGDB feature class, selecting on a LONG field type. Can you include more of your code in your question?2017年03月09日 21:37:51 +00:00Commented Mar 9, 2017 at 21:37
-
2@mmoore In the code you pasted in the comment your list is 'upstreams', but in the question, and in the answer Midavalo gave, the list is 'upstream'.Dan Jurgella– Dan Jurgella2017年03月09日 21:46:10 +00:00Commented Mar 9, 2017 at 21:46
Explore related questions
See similar questions with these tags.
lang-py
COMID
field?in
operator requires a parenthesis-enclosed list).