I have a field for aspect (short integer) with values ranging from 1-4. I want to have 3 additional columns: aspect2, aspect3, aspect4 that are calculated based on the aspect field (for example, if the aspect field is 2, then aspect2 = 1, aspect3 =0 and aspect4 =0).
Here is the code I have:
arcpy.AddField_management(input, 'aspect2', 'SHORT')
arcpy.AddField_management(input, 'aspect3', 'SHORT')
arcpy.AddField_management(input, 'aspect4', 'SHORT')
with arcpy.da.UpdateCursor(input, ['aspect', 'aspect2', 'aspect3', 'aspect4']) as cursor:
for row in cursor:
if cursor[0] == 2:
cursor[1] = 1
cursor[2] = 0
cursor[3] = 0
cursor.updateRow(row)
elif cursor[0] == 3:
cursor[1] = 0
cursor[2] = 1
cursor[3] = 0
cursor.updateRow(row)
elif cursor[0] == 4:
cursor[1] = 0
cursor[2] = 0
cursor[3] = 1
cursor.updateRow(row)
else:
cursor[1] = 0
cursor[2] = 0
cursor[3] = 0
cursor.updateRow(row)
When I run it, I get the following error:
Traceback (most recent call last):
File "<string>", line 19, in <module>
TypeError: 'da.UpdateCursor' object does not support item assignment
I'm not sure if it's a basic syntax issue (it's very possible) or I'm not thinking about it correctly. Any ideas are greatly appreciated.
Solved! Go to Solution.
for row in cursor:
if row[0] == 2:
row[1] = 1
.....
see the code example here
for row in cursor:
if row[0] == 2:
row[1] = 1
.....
see the code example here
Thanks Dan. I figured it would be something embarrassingly obvious like that. It worked great.