I have a shp layer with an attribute table.
One field has the name Nid and the values are strings (e.g. “0150”, “0420”, “3462” …) Now I want to select the rows in the table that match a given Nid value.
This works:
qry = ' "Nid" = \'0150\' '
arcpy.management.SelectLayerByAttribute(layer, "NEW_SELECTION", qry)
But I am really confused by this:
- I know \ is the escape character and used so that 0150 is between two quotes. But why is Nid surrounded by double quotes and not using also \’ ? Note: I tried using \’ Nid\’ and also “0150” but I get errors … I also found examples using [Nid] = \'0150\'' in the web, but that also did not work.
My problem is that I have the values I want to match in the AreaID variable, so AreaID[1]=’0150’ and I am unable to put this in my query:
qry = ' "Nid" = \'AreaID[1]\' ' - does not select anything
qry = ' "Nid" = AreaID[1]' - gives an error
qry = ' "Nid" = "AreaID[1]" ' - gives an error
qry = ' "Nid" = \"AreaID[1]\" ' - gives an error
How does this work?