I have a long code written and this is the last little bit I need and for whichever reason it does not work. This is the simple code.
k=2
arcpy.SelectLayerByAttribute_management("poligon", "NEW_SELECTION", "Id=k")
I tried a lot of different apostrphe, brackets and everything, nothing works. It says a column was specified that does not exist, Error 000358
. But when I replace k
with a number everything is OK. I'm at a loss of what is wrong.
1 Answer 1
Error 000358
means "The SQL expression is invalid", as described in Help - ArcGIS Desktop
Your query, "Id=k"
, means that "get Id
and k
(field) value of a feature, compare both, if both are equal, select that feature". Since there is no k
field in poligon
layer, you encounter an error. Even if k
existed as a field, any feature whose Id
and k
field are equal would be selected.
But you want to select all features meet Id=2
. So, you have to concatenate "Id="
string and integer k
for where clause.
You can do it in different ways:
k = 2
m = 3
"Id=" + str(k) #1 "Id=2"
"Id=%s" %k #2 "Id=2" old style
"Id={}".format(k) #3 "Id=2" new style
"Id={0}".format(k) # by index
## multiple variables ##
"Id=%s%s" %(k, m) # "Id=23"
"Id={}{}".format(k, m) # "Id=23"
# by index, m->0, k->1
"Id={1}{0}_{1}{1}".format(m, k) "Id=23_22"
"Id=" + k # doesn't work. In Python, you cannot concatenate string
# and integer in that way unlike some languages do.
-
Thanks for the answer, I have already tried number 1 and number 2 which didn't work for me, but the number three i havent thaught of and it works perfectly.valsten– valsten2018年04月09日 20:13:14 +00:00Commented Apr 9, 2018 at 20:13
Explore related questions
See similar questions with these tags.
"Id=" + str(k)
as where_clause. Or this link can help."Id={}".format(k)
I like that because it is so easily modified when using text fields to"Id='{}'".format(k)
. I also agree wholeheartedly "that getting some exposure to format statements may benefit you as you move to more complex statements".