UpdateCursor: "TypeError: sequence size must match size of the row" ??

1034
7
Jump to solution
06-08-2020 11:21 AM
AllenDailey1
Occasional Contributor

I'm just learning about Cursors and am trying out the UpdateCursor with fake data in a feature class in a file geodatabase.  I am working towards editing values in existing rows.  I have been able to successfully do this to an extent:  I'm using a counter to give each row a unique value, starting at 1.  The data type is text/string (because this field is for building number, so it could include a building number such as H-101 or something).  

When the counter hits double-digits, i.e. 10, it throws a Type Error - "sequence size must match size of the row."  I read about this error on this Esri page: Error: TypeError: sequence size must match size of the row  

But nothing there seemed to apply to my code.  Does arcpy think that a double-digit number has two pieces, so it cannot be put into one cell??  

Here is my code:

import arcpy
fc = "H:\ActiveShooter\ActiveShooterAutomation\ActiveShooterAutomation.gdb\Fake_AS_buildings"

# This worked only up to 9, assigning building numbers from 1 to 9.  Error after 9:
# "TypeError: sequence size must match size of the row" ???
counter = 0
with arcpy.da.UpdateCursor(fc, ["BUILDING_NUMBER"]) as cursor:
    for row in cursor:
        counter += 1
        row = str(counter)
        print(row, type(row))  # For testing/diagnostic purposes
        cursor.updateRow(row)

Here is what happened in ArcMap.  The cell values were successfully edited/entered, up through 9.

Can anybody help me figure out why this error is occurring?  Thank you!!

Allen

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

Assuming building number is supposed to be a string value, try:

with arcpy.da.UpdateCursor(fc, ["BUILDING_NUMBER"]) as cursor:
    for row in cursor:
        counter += 1
        row[0] = str(counter) # update first element of row
        print(row[0], type(row[0]))  # For testing/diagnostic purposes
        cursor.updateRow(row)‍‍‍‍‍‍

Note line 4, using indexing.

View solution in original post

7 Replies
DanPatterson
MVP Esteemed Contributor

try putting counter += 1 after the cursor.updateRow line


... sort of retired...
0 Kudos
AllenDailey1
Occasional Contributor

Thanks for your reply, Dan! 

I tried that right after reading your reply, and unfortunately it did not change the results.

0 Kudos
DavidPike
MVP Frequent Contributor

You've passed the field in as a list, possibly either remove the []'s or use row[0] =

But I don't think that's actually causing an issue from what you say, what's your field length on building number? seems like values over 2 are too long?

AllenDailey1
Occasional Contributor

Thank you, David!  Based on your suggestion, I tried removing the [] around the field name, and that didn't accomplish anything.  Then I put the [] back on, and I tried adding the index to this line: row[0]=str(counter), and it worked!  Now I am able to change values to be multiple characters.

I thought I wouldn't need to use the [0] because I was only using one field, rather than two, but I suppose it does make sense that it's still necessary, because the field is a list in this code.

The field length is 50.

0 Kudos
RandyBurton
MVP Regular Contributor

Assuming building number is supposed to be a string value, try:

with arcpy.da.UpdateCursor(fc, ["BUILDING_NUMBER"]) as cursor:
    for row in cursor:
        counter += 1
        row[0] = str(counter) # update first element of row
        print(row[0], type(row[0]))  # For testing/diagnostic purposes
        cursor.updateRow(row)‍‍‍‍‍‍

Note line 4, using indexing.

AllenDailey1
Occasional Contributor

Thank you Randy - this solved the problem!

Allen

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

To explain the issue with your original code, you were first setting row as a list returned by the cursor but then re-setting it to be a string two lines later.  By the time you get to updating the cursor, row was no longer a list but a string.  The reason you got a SequenceError instead of a TypeError is that strings and lists are both sequences, so the cursor didn't throw a TypeError.  However, any string with more than 1 character would be like passing a list with more than one item, hence the SequenceError.