使用arcpy UpdateCursor的Python语法错误

时间:2016-01-07 01:36:55

标签: python arcgis arcpy

我是python的新手,希望有一个脚本来查看要素类并比较两个文本字段中的值,然后使用YN填充第三个字段,具体取决于如果值相同或不相同。我想我需要使用带有if语句的UpdateCursor。我尝试了以下但是当我尝试运行它时出现语法错误。我正在使用ArcGIS 10.1,并且知道daCursor更好,但我只是想绕过游标,我想我会尽量保持简单。

#import system modules
import  arcpy
from arcpy import env
import os
import sys


#set environment settings
working_fc = sys.argv[1]
working_gdb = os.path.split(working_fc)[0]


#use an update cursor to populate the field BEC_UPDATED based on the     result of a query 
#query = ("SELECT  * FROM working_fc" "WHERE [BEC_LABEL] = [BEC_V9]")
#if the query is true, then BEC_UPDATED should be popluated with "N"
#if the query is false, then BEC_UPDATED should be populated with "Y"

rows = arcpy.UpdateCursor (working_fc)
for row in rows:
    if row.getValue("BEC_LABEL") == row.getValue("BEC_V9")
    row.BEC_UPDATED = "N"
    else
    row.BEC_UPDATED = "Y"
    rows.updateRow(row)

 print "BEC_UPDATED field populated"

2 个答案:

答案 0 :(得分:0)

你忘记了'if'区块中的冒号和缩进:

rows = arcpy.UpdateCursor (working_fc)
for row in rows:
    if row.getValue("BEC_LABEL") == row.getValue("BEC_V9"):
        row.BEC_UPDATED = "N"
    else:
        row.BEC_UPDATED = "Y"
    rows.updateRow(row)

 print "BEC_UPDATED field populated"

答案 1 :(得分:0)

您的语法错误是由缩进和缺少冒号引起的。 Python对此很挑剔,因此在收到语法错误时请务必检查。

rows = arcpy.UpdateCursor(working_fc)
for row in rows:
    if row.getValue("BEC_LABEL") == row.getValue("BEC_V9"):
        row.BEC_UPDATED = "N"
    else:
        row.BEC_UPDATED = "Y"
    rows.updateRow(row)

将此更改为da.UpdateCursor语法基本相同,但要求您预先指定您感兴趣的属性。这是值得练习的,因为一旦你进入更复杂的脚本,它将变得更容易:)

fieldList = ["BEC_LABEL", "BEC_V9", "BEC_UPDATED"]
with arcpy.da.UpdateCursor(working_fc, fieldList) as cursor:
    for row in cursor:
        if row[0] == row[1]:
            row[2] = "N"
        else:
            row[2] = "Y"
        cursor.updateRow(row)