arcgisarcpy

Python for ArcGIS: add a text field to a shape file, then search one field and if meets a condition, update another field


I'm learning Python (2.7) for ArcGIS (10.8) on my own. I have this problem:

Write a script that adds a text field to the roads.shp feature class called FERRY and populates this field with YES and NO values, depending on the value of the FEATURE field.

After trying to figure it out and checking over the internet I got this script:

from arcpy
from arcpy import env
env.workspace = "C/esriPress/Python/Data/Exercise07/Results" # Set workspace
env.overwriteOutput = True   # overwriting outputs
fc = "roads.shp" # input feature class

newfield = "FERRY"  # new field to be created
fieldtype = "TEXT"  # type of the field that is going to be created
fieldname = arcpy.ValidateFieldName(newfield)  # to determine whether a specific name is valid or not
field_length = 3 # Lenght of th new field to be created
fieldlist = ListFields(fc)  # list the fields in 'fc', it returns a reference number of each object, not the object name.
fieldnames = [] # Create an empty list


# Check whether 'newfield' already exist or not; if not, create it
for field in fieldlist: # For each element in 'fieldlist'...
    fieldnames.append(field.name) #...add in the empty list 'fieldnames' the field name
if fieldname not in fieldnames: # if 'fieldname' don't exists in 'fiednames'...
    arcpy.AddField_management(fc, fieldname, fieldtype, field_length)  # ...Create 'fieldname' in 'fc' with length = 3
    print ("New field has been added")
else:
    print ("Field name already exists.")

ā€‹
# Search in the one field and update other field
fields = ['FEATURE', newfield]  # Create a list that contains the field we apply condition, and  the field we going to update
with arcpy.da.UpdateCursor(fc, fields) as cursor: # Setting the cursor; notice the cursor is made up of two fields
    for row in cursor: # For each row in cursor...
        if row[0] == "Ferry Crossing": #...if row in field 1 = "Ferry Crossing'...
            row[1] = "YES" #... Set that row in field 2 to 'Yes'
        else: # Otherwise...
            row[1] = "NO" # ....Set that row in field 2 to 'No'...
        cursor.updateRow(row) # Update cursor
print ("New field has been added" % newfield)

        else:
            row[1] = "NO"
        cursor.updateRow(row)
print ("Field name already exists" % newfield)

However, when I run the script in PythonWin 2.7.16, I get this error message:

Failed to run script - syntax error - invalid syntax

What is the problem with my script? Is it the right approach?


Solution

  • It appears you have 2 else blocks and an extra cursor.updateRow(row). As a result, your print statement is not indented properly. Try running the following Update Cursor:

    import arcpy
    
    fc = '/path/to/your/geodatabase.gdb/feature_class'
    
    # Add field
    arcpy.management.AddField(fc, "FERRY", field_type = "TEXT")
    
    # Update attributes based on a condition
    # Note row[0] is "FEATURE" and row[1] is "FERRY"
    with arcpy.da.UpdateCursor(fc, ["FEATURE", "FERRY"]) as cursor:
        for row in cursor:
            if row[0] == "ADD YOUR CONDITION HERE":
                row[1] = "YES"
            else:
                row[1] = "NO"
            cursor.updateRow(row)