Problem Executing Loop

457
4
09-23-2011 03:20 AM
JohanOldekop
New Contributor
Hello,

New to python and writing my first scripts. I am trying to build a loop to generate individual polygon shapefiles from a feature layer which contains a whole series of polygons. Here's the script, which doesn't seem to work as well as the error message. I'd be grateful if anyone has any suggestions...

cheers,

Johan

Import Phython functions
import arcpy
from arcpy import env
from arcpy.sa import *

env.workspace = "C:/GTZ Data/PEN_IUCN/PEN_IUCN"

InShapefile = "C:/IUCN Data/AMPHANURA/AMPHANURA_RBS_Clip.shp"

try:
rows = arcpy.SearchCursor (InShapefile)
row = rows.next()

while row:
id= row.getValue ("FID")
outFC=id + ".shp"
sel = str('"FID" = ' " ' "+id+" ' ")

arcpy.Select_analysis(InShapefile, OutFC, sel)
row = rows.next()

except:

arcpy.AddMessage(arcpy.GetMessages(2))
print arcpy.GetMessages(2)

Parsing error <type 'exceptions.IndentationError'>: expected an indented block (line 2)
Tags (2)
0 Kudos
4 Replies
VeraDiaz-Köhli
New Contributor
joldekop;136108 wrote:



.....
while row:
   id= row.getValue ("FID")
   outFC=id + ".shp"
   sel = str('"FID" = ' " ' "+id+" ' ")
  
   arcpy.Select_analysis(InShapefile, OutFC, sel)
   row = rows.next()


>> indent block 2! (after while row:)

I'm using "Pyscripter" to write my scripts. If you forget to indent code there, it's marked as an error.
0 Kudos
Luke_Pinner
MVP Regular Contributor
Please wrap your script in code tags so the indentation is preserved. Especially when your problem IS with indentation...

I had a look at the page source. You don't have an indented block after the except clause:
0 Kudos
JamesHood
Occasional Contributor
Hello, 

New to python and writing my first scripts. I am trying to build a loop to generate individual polygon shapefiles from a feature layer which contains a whole series of polygons. Here's the script, which doesn't seem to work as well as the error message. I'd be grateful if anyone has any suggestions... 

cheers, 

Johan 

Import Phython functions 
import arcpy 
from arcpy import env 
from arcpy.sa import * 

env.workspace = "C:/GTZ Data/PEN_IUCN/PEN_IUCN" 

InShapefile = "C:/IUCN Data/AMPHANURA/AMPHANURA_RBS_Clip.shp" 

try: 
rows = arcpy.SearchCursor (InShapefile) 
row = rows.next() 
  
while row: 
id= row.getValue ("FID") 
outFC=id + ".shp" 
sel = str('"FID" = ' " ' "+id+" ' ") 
  
arcpy.Select_analysis(InShapefile, OutFC, sel) 
row = rows.next() 

except: 

arcpy.AddMessage(arcpy.GetMessages(2)) 
print arcpy.GetMessages(2) 

Parsing error <type 'exceptions.IndentationError'>: expected an indented block (line 2)



Note also that you are creating shapefiles that will be numeric.shp... starting a shapefile with a number generally makes ArcMap sad.


Try also:
id= row.getValue ("FID")
outFC="shape_" + str(id) + ".shp"
sel = str('"FID" = ' " ' "+id+" ' ")
0 Kudos
MichaelStead
Occasional Contributor III
Arcmap is moody in general, somedays it might smile at numeric.shp.....

What is that first line anyways? "Import Phython Functions"
0 Kudos