I have this py script for selecting one point.

# Within selected features, select only those points of a chosen value  
arcpy.SelectLayerByAttribute_management("Stations", "NEW_SELECTION", ' "OID" = 465 ')

That works but every time I want to select a new station then I have to change the station number in the SQL statement.

How can I select a new station as a raw input instead of editing the script every time?

  • 1
    Why do you want to use raw input? If you are running this as a script tool you could set up a parameter that lets you enter one or multiple values, and then plug that into the where clause with something like one of these answers: 1 2 3 – blah238 Jul 8 '13 at 17:46

This should work. It uses Python string formatting to construct the where_clause.

stationNumber = raw_input("Enter the value")
arcpy.SelectLayerByAttribute_management("Stations", "NEW_SELECTION", "OID = {0}".format(stationNumber))

To select multiple OIDs, you could just hardcode it like so:

number1 = 25
number2 = 37
'"OID"=' + str(number1) + ' OR "OID"=' + str(number2)
'"OID"=25 OR "OID"=37'

This becomes a problem when you need to select multiple OIDs, so you can use something like below. I've successfully used this as inputs to cursors and select layer by attribute/location, but it can't be used for anything over a few thousand entries as it causes fatal ArcMap crashes. (I'm not sure what the cut off point is.) If you need to be selecting that many OIDs, it's best to write all your data to a list of lists and filter out what you want in Python.

numberlist = [25,37,29,251,17]
'"OID"=' + ' OR "OID"='.join(map(str, numberlist))
'"OID"=25 OR "OID"=37 OR "OID"=29 OR "OID"=251 OR "OID"=17'

As @nmpeterson pointed out, the following is much better and probably doesn't suffer from the crashes:

numberlist = [25,37,29,251,17]
'"OID" IN ({1})'.format(",".join(map(str, numberlist)))    
'"OID" IN (25,37,29,251,17)'

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.