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

2 Answers 2

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 + "'")
2
  • Thank you so much for your answer. I can do it. Commented Jul 31, 2020 at 3:03
  • Hi! But could you explain to me why we have to use the percentage sign % like that? Commented Jul 31, 2020 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.

Your Answer

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

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