1

I am calculating buffer widths based on the ages of polygons. The buffer widths are stored in a new column in the attribute table. The code is:

with arcpy.da.UpdateCursor(hf,["FEATURE","YEAR","BUff"]) as cursor:
for row in cursor:
    f_type = row[0]
    year = row[1]
    buff = row[2]
    if f_type == "GRASSLAND":
        age = 2012 - int(year)
        if age > 60:
            buff = 0
            cursor.updateRow(row)
        if age <=60:
            buff = (10 * age) / 50
            cursor.updateRow(row)

Why is this code not successfully editing the attribute table?

1

The reason this code runs, but fails to update the attribute table, is due to my use of variables to represent the called attribute values. I attempted to make the code more legible by substituting variables for row[0], row[1], and row[2]. As a result, when I "updated" the values of the Buff attribute using buff = (10 * age) / 50, I was only updating the value of the variable, not the attribute table. Here is the corrected, functional code:

with arcpy.da.UpdateCursor(hf,["FEATURE","YEAR","BUff"]) as cursor:
for row in cursor:
    f_type = row[0]
    year = row[1]
    if f_type == "GRASSLAND":
        age = 2012 - int(year)
        if age > 60:
            row[2] = 0
            cursor.updateRow(row)
        if age <=60:
            row[2] = (10 * age) / 50
            cursor.updateRow(row)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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