Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to create a query for the select by attribute function within a Python script.This is my current code.

    lot = '96'
    dp = 756421

    lotFile = "D:\\NCT_GIS\\DATA\\Regional_55.gdb\\Murray\\cadastre"
    arcpy.MakeFeatureLayer_management(lotFile, "lot_lyr") 
    arcpy.SelectLayerByAttribute_management("lot_lyr", "NEW_SELECTION", "\"LOTNUMBER\" = " + lot + " AND " + "\"PLANNUMBER\" = " + str(dp))

Unfortunately I cannot concatenate and integer and if I convert DP to a string then the query is invalid as the PLANNUMBER field is numeric whilst LOTNUMBER is text.

Does anyone know how to get the variables into the query properly so that I can input any lot and dp as this will eventually become an addin where users can specify which land parcel they are interested in.

share|improve this question

2 Answers 2

Python's format string function (along with triple-quoted strings) is an elegant way to do this:

lot = '96'
dp = 756421

lotFile = "D:\\NCT_GIS\\DATA\\Regional_55.gdb\\Murray\\cadastre"
query_str = """ "LOTNUMBER" = '{0}' AND "PLANNUMBER" = {1} """.format(lot, dp)
arcpy.MakeFeatureLayer_management(lotFile, "lot_lyr", query_str)

Format will string-ify whatever variables you add as arguments and insert them into the spot occupied by {i}, where i is the position of a particular argument in the list of arguments.

Triple-quoting the entire query string allows you to use both double and single quotes without escaping either one.

The two together allow for a highly readable query string, not broken up into a bunch of different parts that are concatenated with +.

One last note: arcpy.MakeFeatureLayer_management() has an optional 3rd argument that allows you to specify a query, thus eliminating the need for the subsequent call to arcpy.SelectLayerByAttribute_management().

share|improve this answer

I think this should work ... I prefer to use combinations of single and double quotes rather than backslashes to escape.

arcpy.SelectLayerByAttribute_management("lot_lyr", "NEW_SELECTION", '"LOTNUMBER" = ' + "'" + lot + "'" + ' AND "PLANNUMBER" = ' + str(dp))

Note that lot is input as a string so can be used as is, but dp which is an integer needs an str function.

share|improve this answer
    
Unfortunately not. It is giving the "cannot concatenate str and int" error. –  user25074 Feb 5 '14 at 23:17
    
set dp = '756421' so it will be a string (I edited @PolyGeo's post--oop s/he beat me to it.). –  Roland Feb 5 '14 at 23:19
    
Tried that but as the field PLANNUMBER is not text, the query is invalid. I somehow need to be able to have a number in this query string. –  user25074 Feb 5 '14 at 23:22
    
Works great! Thanks for all your help! –  user25074 Feb 5 '14 at 23:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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