Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am working with some ArcGIS data and trying to add fields using AddField_management.

>>> arcpy.AddField_management("huc4","Class","TEXT")

I want to populate this new field using the UpdateCursor and two other fields that already exist.

>>> cursor = arcpy.da.UpdateCursor("huc4",["SHAPE_Area","SHAPE_Leng","Class"])
>>> 
>>> for row in cursor:
...   if row[0]/row[1] > 40000:
...         row[2] = "Low"
...         cursor.updateRow([row])
...         
Runtime error 
Traceback (most recent call last):
  File "<string>", line 4, in <module>
TypeError: sequence size must match size of the row
>>> 

I don't really understand this error, but it's happened with every attempt I've made to use the UpdateCursor to populate my new fields.

share|improve this question
    
is it possible you are creating the cursor prior to adding the fields, if so add the field before creating the cursor. I noticed that you are limiting the fields in the cursor and Class is not one of them. From memory the update cursor doesn't like limited fields so much. –  Michael Miles-Stimson Apr 29 at 3:54
2  
I personally dislike using row[1].. syntax. It works fine if you have complete control of the feature class but in the future if the fields for the input don't match exactly with the original you could end up dividing road_name by state_route or something else equally as annoying. Unless you are absolutely certain of the fields I recommend getvalue and setvalue access on row objects - it also makes it easier to see what row.GetValue("Shape_Area") / row.GetValue("Shape_Leng") is trying to achieve. –  Michael Miles-Stimson Apr 29 at 4:03
    
It looks like you are doing this interactively in perhaps the Python window of ArcMap. Would you instead be able to include a code snippet from an IDE like IDLE that we can simply copy, make minor mods to, and then run to try and reproduce your error, please? Before you open the UpdateCursor you may want to list your fields to make sure that what you think they are at that point matches. –  PolyGeo Apr 29 at 4:54
3  
Shouldn't it be cursor.updateRow(row) instead of cursor.updateRow([row])? –  Alex Tereshenkov Apr 29 at 6:21
    
Thank you Michael for the tip re [1] –  michaelgaudern May 4 at 0:40
add comment

1 Answer 1

As Alex Tereshenkov asked, you should be passing the row tuple back directly, not inside of a list (no brackets):

...
        cursor.updateRow(row)

This had nothing to do with adding the field, but the way you were passing the data back. It appeared that you had only passed back a single value and ArcPy was complaining that it needed 3 instead of 1.

share|improve this answer
    
Ah ha. Got the no brackets sorted. Thank you both. –  michaelgaudern May 3 at 22:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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