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.

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.

  • 8
    Try "Id=" + str(k) as where_clause. Or this link can help. – Kadir Şahbaz Apr 8 '18 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. – PolyGeo Apr 8 '18 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. – Michael Stimson Apr 8 '18 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! – RJJoling Apr 8 '18 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". – PolyGeo Apr 8 '18 at 22:45

@Michael explained everything very well in his comment. But to be a proper answer, I will explain a little.

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". Because 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.
  • 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 Apr 9 '18 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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