7

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.

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Apr 8, 2018 at 21:09
5
  • 8
    Try "Id=" + str(k) as where_clause. Or this link can help. Commented Apr 8, 2018 at 21:26
  • Welcome to GIS SE! As a new user be sure to take the Tour. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet rather than a copy/paste from your code and the full error message including the line number that results from running that code snippet. Commented Apr 8, 2018 at 21:33
  • 1
    This is because your query is looking for a field called k, which doesn't exist, hence the field not found error. @KadirŞahbaz is correct, you need to put your variable into the statement either with simple concatenation (as shown "Id=" + str(k)) or "Id={}".format(k) to insert the value of the variable into the string; either would work but I suggest that getting some exposure to format statements may benefit you as you move to more complex statements. Perhaps Kadir, you'd like to expand your comment to an answer. Commented Apr 8, 2018 at 22:05
  • 2
    @KadirŞahbaz Post this as a proper answer with a little explanation on string conversion, so we can give you the proper credits! Commented Apr 8, 2018 at 22:07
  • 1
    The where_clause syntax that I prefer is mentioned in @MichaelStimson's comment: "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". Commented Apr 8, 2018 at 22:45

1 Answer 1

17

Error 000358means "The SQL expression is invalid", as described in Help - ArcGIS Desktop

Your query, "Id=k", means that "get Idand k (field) value of a feature, compare both, if both are equal, select that feature". Since there is no kfield in poligon layer, you encounter an error. Even if k existed as a field, any feature whose Id and kfield 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.
answered Apr 8, 2018 at 23:36
1
  • 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. Commented Apr 9, 2018 at 20:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.