Geographic Information Systems Asked by hồng nguyễn văn on October 1, 2021
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.
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 + "'")
Correct answer by radouxju on October 1, 2021
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.
Answered by PolyGeo on October 1, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP