0

I am new to coding using ArcPy.

My code is:

roadname = "Ly Thuong Kiet"
arcpy.SelectLayerByAttribute_management("inLayer_lyr","NEW_SELECTION",""" "DIA_CHI" LIKE '{0}' """.format(roadname))

This code can run easily. However, I want to choose all the elements belonging to "DIA_CHI" field that have a name in the roadname. Example: "Hem 32 Ly Thuong Kiet" will be contained in ("Ly Thuong Kiet").

When I wrote this code:

arcpy.SelectLayerByAttribute_management("inLayer_lyr","NEW_SELECTION",""" "DIA_CHI" LIKE "%Ly Thuong Kiet")

It can run. But when I use variable , I couldn't use percentage sign (%) in format.

1

not the most "pythonic" solution, but you could simply avoid the .format by using string concatenation

arcpy.SelectLayerByAttribute_management("inLayer_lyr","NEW_SELECTION",""" "DIA_CHI" LIKE '%""" + roadname + "'")
| improve this answer | |
  • Thank you so much for your answer. I can do it. – hồng nguyễn văn Jul 31 at 3:03
  • Hi! But could you explain to me why we have to use the percentage sign % like that? – hồng nguyễn văn Jul 31 at 3:08
0

I would try doing it like this:

arcpy.SelectLayerByAttribute_management("inLayer_lyr","NEW_SELECTION","DIA_CHI LIKE '%{0}'".format(roadname))

Delimiters around field names have not been needed for quite a few versions and dispensing with them simplifies the syntax.

| improve this answer | |

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.