sequence size must match size of the row

1335
9
Jump to solution
10-15-2020 09:08 PM
jayshukla
New Contributor III

Hi, I am looking for some help on proper syntax in ArcGIS Pro/ Python 3. I am trying to read data from a feature class and write to another feature class. The attribute fields are different so I am reading from source and assigning it to output. I get this error at insertCursor.insertRow(newRow).

Appreciate any input.
Thanks

def LoadData(fSource, fTarget):
# setup search cursorfields = ['*']  fldShape = "SHAPE@"# target field mapping fldCountry = 'Country'fldCommodity = "Product"constructionYear = "CNSTR_YEAR"try:      with arcpy.da.InsertCursor(fTarget, ['*']) as insertCursor:         with arcpy.da.SearchCursor(fSource, "*") as cursor:             for row in cursor:                 country = row[cursor.fields.index(fldCountry)]                 geometry = row[cursor.fields.index(fldShape)]                 commodity = row[cursor.fields.index(fldCommodity)]                 constructionYear = row[cursor.fields.index(constructionYear)]                                  newRow = [country, geometry, commodity, constructionYear]                  insertCursor.insertRow(newRow)  except Exception:         print(e.args[0])
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

I need a bit more information on the calculation.  But I would add a field to a targetField list.  Then do the calculation just before the insert row.  The idea would be something like this, but the code is untested and needs modification to do the calculation.

fSource = '' # some source
fTarget = '' # the target

sourceFields = ['SHAPE@', 'Country', 'Product', 'CNSTR_YEAR'] 
targetFields = ['SHAPE@', 'Country', 'Product', 'CNSTR_YEAR', 'CalcField' ] # add calculated field

with with arcpy.da.InsertCursor(fTarget, targetFields) as insertCursor:
    with arcpy.da.SearchCursor(fSource, sourceFields) as cursor:
        for row in cursor:
            newRow = list(row) # get sourceField values
            x = 5+2 # some calculation
            newRow.append(x) # append calculation to end
            insertCursor.insertRow(newRow) # insert newRow to fTarget‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

9 Replies
JoshuaBixby
MVP Esteemed Contributor

The sequence size error is saying you are either trying to insert fewer or more items into a record than what the cursor is set up for.  Since you are using "*" for the fields, which by the way is a poor practice, I can't say for sure what is going wrong with the fields.

jayshukla
New Contributor III

Thanks Joshua, I will try limiting the fields and see it helps. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

This would help with reading

/blogs/dan_patterson/2016/08/14/script-formatting 


... sort of retired...
jayshukla
New Contributor III
def LoadData(fSource, fTarget):

  # setup search cursor
fields = ['*']

fldShape = "SHAPE@"

# target field mapping
fldCountry = 'Country'
fldCommodity = "Product"
constructionYear = "CNSTR_YEAR"

try:
with arcpy.da.InsertCursor(fTarget, ['*']) as insertCursor:
  with arcpy.da.SearchCursor(fSource, "*") as cursor:
  for row in cursor:
  country = row[cursor.fields.index(fldCountry)]
geometry = row[cursor.fields.index(fldShape)]
commodity = row[cursor.fields.index(fldCommodity)]
constructionYear = row[cursor.fields.index(constructionYear)]

newRow = [country, geometry, commodity, constructionYear]

insertCursor.insertRow(newRow)

except Exception:
  print(e.args[0])
0 Kudos
jayshukla
New Contributor III

This looks better formatting, Thanks Dan for pointing this out.  

0 Kudos
RandyBurton
MVP Regular Contributor

As Joshua Bixby‌ states, using a wildcard for the  fields is a bad idea.  In your case, you cannot be sure the order you are assembling the row matches the order the data was selected.  Also, for testing you should avoid a try/except block so the error messages will be clear and you can see what exceptions you may need to guard against.

Can we assume that the field names are identical (as well as field type and length)  between the source and target features?  If so, you might test the following with copies of your data:

fSource = '' # some source
fTarget = '' # the target

fields = ['SHAPE@', 'Country', 'Product', 'CNSTR_YEAR'] # assuming same name used in both target/source

with with arcpy.da.InsertCursor(fTarget, fields) as insertCursor:
    with arcpy.da.SearchCursor(fSource, fields) as cursor:
        for row in cursor:
            insertCursor.insertRow(row)
‍‍‍‍‍‍‍‍‍
jayshukla
New Contributor III

Thanks Randy, Your code works great. I have one calculated value that doesn't exist in source data, how do I update that field in target using this construct. 

0 Kudos
RandyBurton
MVP Regular Contributor

I need a bit more information on the calculation.  But I would add a field to a targetField list.  Then do the calculation just before the insert row.  The idea would be something like this, but the code is untested and needs modification to do the calculation.

fSource = '' # some source
fTarget = '' # the target

sourceFields = ['SHAPE@', 'Country', 'Product', 'CNSTR_YEAR'] 
targetFields = ['SHAPE@', 'Country', 'Product', 'CNSTR_YEAR', 'CalcField' ] # add calculated field

with with arcpy.da.InsertCursor(fTarget, targetFields) as insertCursor:
    with arcpy.da.SearchCursor(fSource, sourceFields) as cursor:
        for row in cursor:
            newRow = list(row) # get sourceField values
            x = 5+2 # some calculation
            newRow.append(x) # append calculation to end
            insertCursor.insertRow(newRow) # insert newRow to fTarget‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
jayshukla
New Contributor III

Thanks Randy, Joshua for your help on this. this works like a charm, really appreciate your help on this. 

0 Kudos