使用 python 将 shp 属性数据添加到列

Add shp attribute data to column with python

如果 table 中的特征列是渡口,那么我需要在我已经添加到 table 的渡口列中添加是。如果功能栏没有说渡口,我需要在渡口栏中添加否。这是我遇到问题的代码块。有什么建议么?是的,我是 python 的新手,任何建议或提示都将不胜感激,因为它会帮助我学习。干杯。

#need to update FERRY column with yes or no values if ferry crossing is available in feature field
delimfield = arcpy.AddFieldDelimiters(fc, "FERRY")
cursor = arcpy.da.UpdateCursor(fc, ["FERRY"])
for row in cursor:
    if row[0] == "ferry crossing":
        cursor.updateRow("FERRY")
        print "YES"
    if row[0] is not "ferry crossing":
        cursor.updateRow("FERRY")
        print "NO"

del row
del cursor

#if feature is ferry crossing == YES in FERRY field
#if feature is not ferry crossing == NO in FERRY field

您需要抓住光标中的两个字段:测试的 feature 字段和要更新的 FERRY 字段。然后,您现有的代码将正确测试,但您需要修改它以执行更新。使用字段的列表位置在行对象中访问它们。

with arcpy.da.UpdateCursor(fc, ["FEATURE", "FERRY"]) as cursor:
    for row in cursor:
        if row[0] == "ferry crossing": # test the FEATURE field
            row[1] = "FERRY" # set the FERRY field
            print "YES"
        else:
            print "NO"
        cursor.updateRow(row) # commit the changes