Python Forum
Use Select_analysis in a loop
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use Select_analysis in a loop
#1
How can I use select_analysis in a loop?
I need to select polygons one by one and create separate shapefiles from them.
I tried the following codes but I was not successful

import arcpy

# Local variables: This is the Grid
Mapsheet_grid_50K = "Mapsheet_grid_50K"

rows = arcpy.SearchCursor(Mapsheet_grid_50K)
row = rows.next()
while row:

   Expression = "[MAP_TILE] =str(row)"
   Mapsheet_grid_50K_Select_shp = "F:\\Canada_DSM_ForTestOnly\\PersonalData&Maps\\map_Tiles\\Mapsheet_grid_50K_"+ str(row)+".shp"
   arcpy.Select_analysis(Mapsheet_grid_50K, Mapsheet_grid_50K_Select_shp, Expression)

   row = rows.next()
Reply
#2
What exactly does 'not successful' mean? Not an expert on arcpy, but I'm pretty sure [MAP_TILE] =str(row) condition would not return any results. eventually
Expression = "[MAP_TILE] ={}".format(row)
may work, but not sure
Reply
#3
also, I would do
rows = arcpy.SearchCursor(Mapsheet_grid_50K)
for row in rows:
    Expression = "[MAP_TILE]={}".format(row)
    Mapsheet_grid_50K_Select_shp = "F:\\Canada_DSM_ForTestOnly\\PersonalData&Maps\\map_Tiles\\Mapsheet_grid_50K_"+ str(row)+".shp"
    arcpy.Select_analysis(Mapsheet_grid_50K, Mapsheet_grid_50K_Select_shp, Expression)
Reply
#4
Not successful means, I tried to iterate through the rows by using the codes, but I always receive errors, and the script does not work.

Thanks for your help. I will try the changes.
Reply
#5
That is why I asked-if you get errors, post the FULL traceback in error tags. That will help us to help you.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020