Skip to main content
 首页 » 编程设计

python 2.7 : ArcPy cursor while loop

2025年05月04日12xing901022

我正在尝试使用 Python 中的 while 循环从属性表中打印条目,特别是高中设施名称。我可以使用 for 循环打印出高中名称,但我在 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 循环工作?

请您参考如下方法:

我想通了。我在 row.next 之后忘记了 ()。