2015-07-19 75 views
2

我試圖從屬性表中使用Python中的while循環打印條目,特別是高中的設施名稱。我可以使用for循環打印出高中名稱,但我錯過了while循環的一些內容。Python 2.7:ArcPy遊標while循環

#code using for loop: 
import arcpy 


try: 

    work = raw_input("Enter the full workspace path: ") 
    arcpy.env.workspace = work 

    fcs = "Schools.shp" 

    whereClause = "\"FACILITY\" = 'HIGH SCHOOL'" 
    searchCurs = arcpy.SearchCursor(fcs, whereClause, "", "", "") 
    row = searchCurs.next() 
    row.Name 
    for row in searchCurs: 
     print row.Name 

except Exception as e: 
    print "Error: " + str(e) 
    print arcpy.GetMessages() 
    arcpy.AddError(e) 


#code using while loop: 

import arcpy 

try: 

work = raw_input("Enter the full workspace path: ") 
arcpy.env.workspace = work 

fcs = "Schools.shp" 
field = "FACILITY" 

whereClause = "\"FACILITY\" = 'HIGH SCHOOL'" 
searchCurs = arcpy.SearchCursor(fcs, whereClause, "", "", "") 
row = searchCurs.next() 

while row: 
    print(row.getValue(field)) 
    row = searchCurs.next 


except Exception as e: 
    print "Error: " + str(e) 
    print arcpy.GetMessages() 
    arcpy.AddError(e) 

for循環腳本的工作原理。如何讓while循環正常工作?

回答

1

我想通了。我在row.next後忘了()。