我正在尝试使用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之后忘记了()。

关于python - Python 2.7:ArcPy游标在循环中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31504281/

10-12 20:39