3

I have a feature class and I want to move its objects based on two fields in it I used the following code but get error:

with arcpy.da.UpdateCursor(fc2,["SHAPE@XY","centerpo_4","centerpo_5"]) as cursor:
for row in cursor:
    XM=row[1]
    YM=row[2]

    cursor.updateRow([row[0][0]+ XM,row[0][1]+YM])

The columns "centerpo_4","centerpo_5" are two fields within the feature class. When I run the code I encounter with following error:

Traceback (most recent call last):


File "C:\Users\Rezars\Desktop\test2.py", line 34, in <module>
cursor.updateRow([row[0][0]+ XM,row[0][1]+YM])
TypeError: sequence size must match size of the row

The number of objects in the table are 2893 (FID starts from 0)which all three columns have the same row numbers(2893).

This the images of attribute table of my feature class

  • It simply expects a full list of row fields that you defined. row [0] i.e. SHAPE@XY gives/expects you a xy tuple, be aware. – fatih_dur Jun 11 '16 at 15:52
  • I have got this code from :gis.stackexchange.com/questions/65959/… – Khandan Jun 11 '16 at 15:55
  • it works when I use fixed values but gives error when I want to use the values in the table as variables. – Khandan Jun 11 '16 at 15:56
5

What you're trying to do won't work as you're not passing back the same information as you asked in your cursor - ["SHAPE@XY", "centerpo_4", "centerpo_5"] - it is expecting three values back. This is why you're getting the error

TypeError: sequence size must match size of the row

First you need to pass your new XY to row[0] as a tuple. As your other two values are unchanged, you can just modify the SHAPE@XY tuple value and pass the others back unchanged by passing the entire row object. Untested, but I would try something like the following:

with arcpy.da.UpdateCursor(fc2, ["SHAPE@XY", "centerpo_4", "centerpo_5"]) as cursor:
for row in cursor:
    newX = row[0][0] + row[1]
    newY = row[0][1] + row[2]
    row[0] = (newX, newY)

    cursor.updateRow(row)
  • It worked and I learned some thing from you. I really appreciate for your help dear Midavalo – Khandan Jun 12 '16 at 5:14

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.