User Input into Selection Expression

1721
1
10-20-2011 01:07 PM
SheriNorton
Occasional Contributor II
I am trying to write a script that will simply get (1) a county name and (2) and tax parcel ID. These values (stored as variables) need to be used in a SQL expression for the Select by Attribute dialog. This model needs to run in ArcMap, so I'm not sure if the "input" is correct usage and whether I've properly written the SQL expression to handle the variables (county name, parcel ID).
________________________________

# Import modules
import arcpy
import sys


# Set local variables
parcels="Parcels"

# Ask user for the County and Parcel ID to search
county = input("Please type either WARREN, WASHINGTON, OR ESSEX: ")
key = input("Please type the Parcel ID: ")

# User selects COUNTY and then Print Key value, Select by Attribute tool runs
    arcpy.SelectLayerByAttribute_management(Parcels, "NEW_SELECTION", "[COUNTY] = 'county' AND [PRINT_KEY] = 'key'")
Tags (2)
0 Kudos
1 Reply
JakeSkinner
Esri Esteemed Contributor
I believe you will want to use the 'raw_input' function to do this.  Here is an example:

import arcpy, os
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"
env.overwriteOutput = 1

county = raw_input("Please type either WARREN, WASHINGTON, OR ESSEX: ")
key = raw_input("Please type the Parcel ID: ")

# User selects COUNTY and then Print Key value, Select by Attribute tool runs
arcpy.MakeFeatureLayer_management("Parcels", "Parcels_feat")
arcpy.SelectLayerByAttribute_management("Parcels_feat", "NEW_SELECTION", "COUNTY = " + "'" + county + "'")
arcpy.SelectLayerByAttribute_management("Parcels_feat", "NEW_SELECTION", "PARCELS_ID = " + key)
arcpy.CopyFeatures_management("Parcels_feat", "Parcels_Selection")
print "Executed succesfully"


This will prompt the user for a county name and a parcel ID.
0 Kudos