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
1 Answer 1
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
-
2This 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– mr.adam2015年04月17日 13:42:10 +00:00Commented Apr 17, 2015 at 13:42
-
Thank you Mr.adamn, Now I see why I was getting that error message. You were so helpful!!!!!Keri– Keri2015年04月17日 21:12:05 +00:00Commented Apr 17, 2015 at 21:12
lang-py
del row
anddel cursor
are not needed when using a cursor in awith
statement.