0

My code is very simple and it should work, but it doesn't. Perhaps another set of eyes can solve my issue... I created new field (this part works), then look up value of another field and depending on it, update field "Surface" with either 3 or 2 (in my case value stays "null", but no error).

here the code:

import arcpy
from arcpy import env
arcpy.env.workspace = arcpy.GetParameterAsText(0)
arcpy.env.overwriteOutput = True

field5 = "SURFACE"

arcpy.AddField_management(input_fc, field5, "SHORT", "", "", "", "", "","", "Dm_VehicleCapacity")

fields2 = ['VehicleCarryingCapacityCurrent', 'SURFACE']
with arcpy.da.UpdateCursor(input_fc, fields2) as cursor:
    for row in cursor:
        if fields2[0] == 9:
            fields2[1] = 3
            cursor.updateRow(row)
        else:
            fields2[1] = 2
            cursor.updateRow(row)

1 Answer 1

6

you use fields2 (the list of field names) instead of row (the actual record). You should query and modify the row of your cursor (with row[0] the value of 'VehicleCarryingCapacityCurrent' and row[1] the value of 'SURFACE')

fields2 = ('VehicleCarryingCapacityCurrent', 'SURFACE')
with arcpy.da.UpdateCursor(input_fc, fields2) as cursor:
    for row in cursor:
        if row[0] == 9:
            row[1] = 3
        else:
            row[1] = 2
        cursor.updateRow(row)
2
  • That actually didn't work. Comes up with an error.
    – lida
    Apr 5, 2016 at 10:38
  • 1
    what is your error message ?
    – radouxju
    Apr 5, 2016 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.