1

I am trying to split a field which contains, e.g., 2-10 and get the 2. The print statement produces the value 2 which is what I expect. I am getting a TypeError:

TypeError: 'Row' object does not support item assignment

Where am I going wrong; it appears as though I am following documentation and other answers on stack exchange.

ArcGIS 10.3.1, Python 2.7

cursor = arcpy.UpdateCursor(feature, ['CUT_YEAR1'])
for row in cursor:
    print str(int(round(int((((row.getValue("cut_period")).replace(" ", "")).split('-'))[0]))))
    row[0] = str(int(round(int((((row.getValue("cut_period")).replace(" ", "")).split('-'))[0]))))
    cursor.updateRow(row)
  • 2
    I'm amazed you get '2' at all. The cursor requests a field named cut_year1, yet you try to read value cut_period from the row. I think it's less complicated to simply get the first value from the row object. What about row[0] = str(row[0]).split("-")[0].strip()? – Berend Feb 9 '17 at 15:59
3

Your are confusing the syntax of arcpy.da.UpdateCursor with arcpy.UpdateCursor:

Use row.setValue as per the documentation for arcpy.UpdateCursor.

  • 2
    Please edit your answer to include links to ArcGIS Desktop help rather than ArcGIS Pro. While they are probably the same, there can be differences, particularly in code samples. – Midavalo Feb 9 '17 at 16:10
  • 1
    I ended up having to use this answer in combo with @Berend Thanks – D_C Feb 9 '17 at 16:29
1

The error TypeError: 'Row' object does not support item assignment is telling you that with an arcpy.UpdateCursor() you cannot assign values to the row items as you are trying to do with

row[0] = ...

This is valid syntax in an arcpy.da.UpdateCursor(). I would recommend you put your calculation into a new variable so you can print and update and only enter the calculation once (to keep it tidier).

with arcpy.da.UpdateCursor(feature, ['CUT_YEAR1', 'cut_period']) as cursor:
    for row in cursor:
        cp = round(int(row[1].split('-')[0].replace(" ", ""))
        print cp
        row[0] = cp
        cursor.updateRow(row)

If you still need to convert to string (I'm not sure you do need to) you can just put the str() tag around the variable:

with arcpy.da.UpdateCursor(feature, ['CUT_YEAR1', 'cut_period']) as cursor:
    for row in cursor:
        cp = round(int(row[1].split('-')[0].replace(" ", ""))
        print cp
        row[0] = str(cp)
        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.