4

I am trying to use an UpdateCursor fill in a field but I keep getting an error, can anyone tell me what I am doing wrong? My code is below:

import arcpy

fc= "C:\Users\Keri\Desktop\GEP_690\Project Data\FINAL_PROJECT.gdb\NYC_Demographics_2010"
with arcpy.da.UpdateCursor(fc, ["Total1","White","Pct_White"]) as cursor:
    for field in cursor:
        if field[0]==0:
            field[2]=0
        else:
            field[2] = field[1] / field[0]
        cursor.updateRow([field])
print "Complete."

del row

del cursor

This is the error I keep getting:

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\GEP_662\Lab9.py", line 15, in <module>
    cursor.updateRow([field])
TypeError: sequence size must match size of the row
  • As a side note, del row and del cursor are not needed when using a cursor in a with statement. – Aaron Apr 17 '15 at 13:24
  • Thank you for your insight, I did not know that, just learning python – Keri Apr 17 '15 at 21:08
7

Use the cursor.updateRow(field) instead of cursor.updateRow([field]). You should supply an object, not the list.

  • 2
    This will solve the problem, but to be a little more specific, field already is a list (an update cursor returns a set of lists, one per row). When you write [field] you are actually passing a list with one a single entry, and that entry is a list, e.g. [['field1','field2']]. – mr.adam Apr 17 '15 at 13:42
  • Thank you Mr.adamn, Now I see why I was getting that error message. You were so helpful!!!!! – Keri Apr 17 '15 at 21:12

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.