Select tool ArcPy SQL expression

394
2
10-02-2023 11:57 AM
Labels (3)
BergstromGIS
New Contributor II

I am trying to write a script using the select tool to select only those rows with the field description "Seaplane Base". I can get the other value, "Airport" to work, but when I run the script, it gives me an empty table in the new file when selecting "Seaplane Base". 

 

 

import arcpy
arcpy.env.workspace = "###"
infc = "airports.shp"
outfc = "airports_sea.shp"
delim_field = arcpy.AddFieldDelimiters(infc, "FEATURE")
sql_exp = delim_field + " = 'Seaplane Base'"
arcpy.Select_analysis(infc, outfc, sql_exp)

 

 

 Please help

2 Replies
VinceAngelo
Esri Esteemed Contributor

Best practice is to use str.format() (or f"") to layout your expression (and avoid error-prone string math). If you're having difficulty, you should always print the resulting parameters. And test the resulting statement from the UI. Without seeing the contents of the table or the resulting expression, there isn't much we can do to help.

- V 

RobertKrisher
Esri Regular Contributor

Agree with @VinceAngelo, I've been burnt many times by not testing my expressions in the application first and realizing I've made a mistake in my expression or pointed the tool at the wrong dataset. For reference, using a f"" to set your expression would look like the following:

sql_exp = f"{delim_field} = 'Seaplane Base'"