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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 17, 2015 at 13:00
2
  • As a side note, del row and del cursor are not needed when using a cursor in a with statement. Commented Apr 17, 2015 at 13:24
  • Thank you for your insight, I did not know that, just learning python Commented Apr 17, 2015 at 21:08

1 Answer 1

7

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

answered Apr 17, 2015 at 13:05
2
  • 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']]. Commented Apr 17, 2015 at 13:42
  • Thank you Mr.adamn, Now I see why I was getting that error message. You were so helpful!!!!! Commented Apr 17, 2015 at 21:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.