OK - so this is what I've come up with so far. It kind of works. Its going through the whole process but only giving me back the first county that I list as an argument. For example, when I type in county names in the arguments dialogue box I've tried typing it "ASHE";"AVERY" as well as "ASHE" "AVERY" and the script only takes in ASHE. I'm assuming this might be a syntax issue? Here is my current code:
def whereClause(table, field, values):
"""Takes a semicolon-delimited list of values and constructs a SQL WHERE
clause to select those values within a given field and table."""
# Add field delimiters
fieldDelimited = arcpy.AddFieldDelimiters(arcpy.Describe(table).path, field)
# Split multivalue at semicolons and strip quotes
valueList = [value[1:-1]
if (value.startswith("'") and value.endswith("'"))
else value for value in values.split(';')]
# Determine field type
fieldType = arcpy.ListFields(table, field)[0].type
# Add single-quotes for string field values
if str(fieldType) == 'String':
valueList = ["'%s'" % value for value in valueList]
# Format WHERE clause in the form of an IN statement
whereClause = "%s IN(%s)" % (fieldDelimited, ', '.join(valueList))a
return whereClause
def outName(input,post="Out",fileExtension="shp"):
"""Returns output name."""
outName=os.path.basename(input).split(".")[0]+post+"."+fileExtension
return outName
Select County(s)
InputFC = "C:/Project/ToolData/CountyBoundary.shp"
Field = "CO_NAME"
Counties = arcpy.GetParameterAsText(0) #user selects counties from choice list
SQL=whereClause(InputFC,Field,Counties)
OutputFC=outName(InputFC,"_Select")
arcpy.Select_analysis(InputFC,OutputFC,SQL)
count=int(arcpy.GetCount_management(OutputFC).getOutput(0))
print OutputFC, " was created, and contains the following", count,"counties:"
sc = arcpy.SearchCursor(OutputFC)
for line in sc:
print line.CO_NAME