1

Basically I want to run a select analysis on each individual row in a shapefile, then output unique shapefiles. After, I want to use each shapefile that's been output and stored to clip one particular shapefile. Right now my code doesn't do anything and I'm not sure why. Here's what I have:

import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
outputpath = "C:/Users/Documents/HWGDB.gdb/Co_"
clipper = "C:/Users/Documents/HW2_shpfiles/OuterCountiesFull.shp"
ABV_list = ["ClT", "LBT", "FT", "CnT", "MrT", "AB", "CT", "MtB", "AkB", "DB", "EbT", "PT", 
            "WCT", "BT","LT", "PsT", "DT", "EDT", "EnT", "PvT", "QB", "SB", "ST", "WET", 
            "CyT", "WDT", "SsT", "CsT","BrT", "LB", "WwT", "MB", "CrB", "SdT", "EET",
            "THB", "ECT", "MJT", "EGT", "MJB", "RT", "EB","ET", "ET", "NHB", "EbB"]
for abv in ABV_list:
   arcpy.Select_analysis(clipper,outputpath + abv, "'ABBREVIATI = ' + abv")
      print "complete"
4
  • By "doesn't do anything" do you mean each output is the same as its input, no output, an error, or something else?
    – PolyGeo
    Dec 10, 2016 at 19:17
  • @PolyGeo there's no output. When I manually put in an element from the list it works fine but once I involve the list the code runs without error but doesn't output any shapefiles. Dec 10, 2016 at 23:03
  • Your code indentation looks wrong. Are you sure that you are running precisely the code snippet that you have presented? If so, how many times does the word "complete" print?
    – PolyGeo
    Dec 10, 2016 at 23:35
  • You should not need to actually output the unique shapefiles unless you have an explicit reason. Once you select the individual row any tool you use will just use that row/shape. Have you considered using a search cursor to iterate through the rows and clip the desired shapefile with each selected row?
    – timpjohns
    Dec 14, 2016 at 21:15

2 Answers 2

1

assuming your last argument into the select_analysis tool is this where clause query: "'ABBREVIATI = ' + abv"

the iterator from the for loop aka abv is not being properly inserted into the query for each iteration. your use of it is just a string. try this:

"ABBREVIATI  = '{}'".format(abv)
4
  • This hasn't changed anything. The result is still nothing. no errors but also no shapefile outputs. Dec 10, 2016 at 23:31
  • 1
    @SteeleJohnson The syntax of this where_clause looks right to me whereas the one you have looks wrong.
    – PolyGeo
    Dec 10, 2016 at 23:36
  • @ziggy better to put index inside the braces in forrmat specifier like {0}
    – Learner
    Dec 11, 2016 at 16:19
  • This has actually worked for the first piece after also fixing the indents. Updating question with similar naming question for clip_analysis. May just ask a new question. Dec 14, 2016 at 4:43
0

The issue is how you've set your output path variable. This doesn't seem right.

I would remove the "Co_". The actual path should be

outputpath = "C:/Users/Documents/HWGDB.gdb"

I would also use Os.path.join as a way to join paths. Also, it's good practice to set SQL variables before using them.

Therefore try this:

import arcpy, os 
from arcpy import env
arcpy.env.overwriteOutput = True
outputpath = "C:/Users/Documents/HWGDB.gdb"
clipper = "C:/Users/Documents/HW2_shpfiles/OuterCountiesFull.shp"
ABV_list = ["ClT", "LBT", "FT", "CnT", "MrT", "AB", "CT", "MtB", "AkB", "DB", "EbT", "PT", 
            "WCT", "BT","LT", "PsT", "DT", "EDT", "EnT", "PvT", "QB", "SB", "ST", "WET", 
            "CyT", "WDT", "SsT", "CsT","BrT", "LB", "WwT", "MB", "CrB", "SdT", "EET",
            "THB", "ECT", "MJT", "EGT", "MJB", "RT", "EB","ET", "ET", "NHB", "EbB"]

for abv in ABV_list:
    sql = """ "ABBREVIATI  = '{}'".format(abv)"""
    arcpy.Select_analysis(clipper, os.path.join(outputpath, abv), sql)
    print "complete"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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