In the select by attribute how do I replace "AADT", with a predefined string variable?

I don't understand the pattern in "\"AADT\">=" , can you please explain why is it that way when exported from arcMap model builder?

arcpy.SelectLayerByAttribute_management(inFeatures,"NEW_SELECTION","\"AADT\" >="+str(minval)+ "AND \"AADT\" <"+str(maxval))

The "\" is an escape character which is used to be able to use the quotes (" symbol) in the query.

Read here https://gis.stackexchange.com/a/149676/14435 about using field delimiters (will fix the right quotes if any) and use Python str.format for cleaner code . In your case, the string would be something like:

var_name = "AADT"
where_query = """ "{0}" >= {1} AND "{0}" < {2}""".format(var_name,str(minval),str(maxval))
| improve this answer | |
  • I noticed if you use three quotes you don't need to put "\" before quotes within the string . – Solaire Jun 17 '15 at 18:02
  • your where_query is invalid :( @Alex Tereshenkov – Solaire Jun 17 '15 at 19:43
  • 1
    I apologize, I forgot to use the formatting for the variable name; have edited my answer. – Alex Tereshenkov Jun 17 '15 at 20:10

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.