Search code examples
pythoncursorarcgisarcpy

Add shp attribute data to column with python


if feature column in table is ferry crossing then I need to add Yes to the Ferry column that I have already added to the table. If feature column does not say ferry crossing I need to add No to the ferry column. Here is the chunk of code that I am having an issue with. Any suggestions? Yes I'm new to python, any advice or hints would be greatly appreciated as it will help me learn. Cheers.

#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

Solution

  • You need to grab both fields in your cursor: the feature field for your test and the FERRY field that you want to update. Then, your existing code will properly test, but you'll need to modify it to perform the update. Use the list positions of the fields to access them in the row object.

    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