3

This question already has an answer here:

I am working on a simple script tool that selects features from a .shp and creates a new output with only those features. Basically the user specifies(from a drop down list) the county and the preserve and the tool just does a select from a .shp with all the counties and the same for the preserves. I think the problem lies in my syntax, The tool runs without errors but gives me empty outputs. However if I test it without the user input and just put a county name and preserve name in the SQL statement it works which leads me to believe Im not telling the script to use the user input properly. Here is the select part of my script:

#Variables/Arguments
CNTY = "C:\\CRTOOL\\Data\\FL_Counties.shp"
AP = "C:\\CRTOOL\\Data\\FL_AP.shp"
Select_County = arcpy.GetParameterAsText(0)
Select_AP = arcpy.GetParameterAsText(1)

# Select
arcpy.Select_analysis(CNTY, "C:\\CRTOOL\\Data\\Your_County.shp", "\"NAME\" = '%Select_County%' ")
# Select
arcpy.Select_analysis(AP, "C:\\CRTOOL\\Data\\Your_AP.shp", "\"LONG_NAME\" = '%Select_AP%' ")

Here are tool parameters(I have tried them with and without the ' ' in the filter list: enter image description here

UPDATE: WORKING SCRIPT

#Local Variables/Arguments
CNTY = "C:\\CRTOOL\\Data\\FL_Counties.shp"
AP = "C:\\CRTOOL\\Data\\FL_AP.shp"
Select_County = arcpy.GetParameterAsText(0)
Select_AP = arcpy.GetParameterAsText(1)
where = '"NAME" = ' + "'%s'" %Select_County
whereB = '"LONG_NAME" = ' + "'%s'" %Select_AP

# Process: Select
arcpy.Select_analysis(CNTY, "C:\\CRTOOL\\Data\\Your_County.shp", where)
# Process: Select
arcpy.Select_analysis(AP, "C:\\CRTOOL\\Data\\Your_AP.shp", whereB )

marked as duplicate by blah238, Paul, Curlew, PolyGeo, MappaGnosis Oct 15 '13 at 21:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @blah238 I'm trying that solution now will update once finished – landocalrissian Oct 15 '13 at 17:00
3

Please see this earlier answer: How do I include a variable in the where clause of arcpy.Select_analysis?

However, to point out the specific issue here, you are trying to format the string using an unsupported syntax.

The line "\"NAME\" = '%Select_County%' " will simply result in the string "NAME" = '%Select_County%'. No string interpolation has been invoked.

In Python there are two types of string formatting available:

  • The older modulo-style formatting, which looks like this:

    "Hello, %s" % name
    
  • And the newer str.format(), which is more flexible and therefore recommended in Python 2.6 and up:

    "Hello, {0}".format(name)
    

    Or, using keyword arguments:

    "Hello, {name}".format(name=name)
    
  • Great I got it to work I figured it was reading it like that due to a syntax error. Thanks for clarifying and relaying the older post I must have looked at 5 posts and never saw that one. Anyway ill upload my updated working script I used the older modulo form because thats what was in the old post and I hadnt yet read your new answer but it worked well so I didnt change it to the new format. Thanks so much. – landocalrissian Oct 15 '13 at 17:32

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