Select to view content in your preferred language

UpdateCursor - sequence size?

1847
4
Jump to solution
10-29-2014 08:32 AM
BenLeslie1
Frequent Contributor

I'm trying to use UpdateCursor to insert some text into an attribute.  The attribute is a text field ten characters in length.  However the script will only work for values of one character in length.

i.e. if MyValue = "A" the script works

but if MyValue = "All" the error says 'sequence size must match size of the row'

with arcpy.da.UpdateCursor(MyFeatureLyr, MyAttribute) as cursor:

     for row in cursor:

          cursor.updateRow(MyValue)

0 Kudos
1 Solution

Accepted Solutions
DallasShearer
Deactivated User

the updateRow needs to be sent a tupple even if it's only one variable being sent.

change the updaterow to this and it should work

cursor.updateRow((MyValue,))

View solution in original post

0 Kudos
4 Replies
DallasShearer
Deactivated User

the updateRow needs to be sent a tupple even if it's only one variable being sent.

change the updaterow to this and it should work

cursor.updateRow((MyValue,))

0 Kudos
DanPatterson_Retired
MVP Emeritus

The help files says lists or tuples can be used for parameters that require them, see da.UpdateCursor help   if so, I would do it explicitly using the list and tuple methods

>>> a= list('astring')

>>> a

['a', 's', 't', 'r', 'i', 'n', 'g']

>>> b = tuple('astring')

>>> b

('a', 's', 't', 'r', 'i', 'n', 'g')

>>>

or in your case list(MyValue) or tuple(MyValue) in the appropriate line(s)

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I don't think using the list or tuple function in the way described here will yield the desired result.  In the original code snippet, I understand MyAttribute to be a single field/attribute, and that the goal is to populate that attribute with "ALL" or some other multi-character string.  Since a multi-character string is iterable, passing MyValue to the tuple function will result in a tuple with multiple single-character items instead of a tuple with a single multi-character items, as demonstrated.

The suggestion by Dallas Shearer‌ works since it passes the update cursor a single item multi-character tuple, which is what I think the OP is after.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I agree...I guess my point wasn't clear...sorry...
Manually create the tuple from a string, requires entering the , directly

>>> a = ("astring",)  # create the tuple directly

>>> a

('astring',)

If you have a list of string already

>>> a = ["astring"]

>>> b = tuple(a)

>>> b

('astring',)

>>>

If you just have a string, in either case of the tuple or list, you still have to add the string to it otherwise the string is treated as the iterable

0 Kudos