UpdateCursor sequence error

2517
14
Jump to solution
02-13-2021 08:56 AM
AdamG2
by
New Contributor III

Hey community,

Very infrequent poster here, so I'll try my very best to explain this with as much detail as possible. 

I'm trying to write a bespoke script tool for ArcGIS Pro (2.7) using Python (3.7).

I'm trying to use an UpdateCursor to update the rows of a .dbf table (outDBFTable). But when I run the script, I get the classic error "TypeError: sequence size must match size of the row". But when diagnostics 2 and 4 run, it appears the rows do in fact have the same number of elements. 

outDBFTable attributes:

OBJECTIDfield1field2field3field4...
1<Null><Null>...  
2<Null>...   
3...    
4     
5     
6     
7     

 

rasTable attributes:

OBJECTIDCount
1<number>
2...
3 
4 
5 
6 
7 

 

 

...

counter2 = 0
with arcpy.da.SearchCursor(inPoly,"SHAPE@") as sCursor2:
    for sRows2 in sCursor2:
        maskExtract = arcpy.sa.ExtractByMask(reclassRas,sRows2)
        rasTable = arcpy.management.BuildRasterAttributeTable(maskExtract) 
        arcpy.AddMessage(rasTable) # diagnostics 1

        with arcpy.da.SearchCursor(rasTable,("Value","Count")) as sCursor3:
            for sRows3 in sCursor3:
                arcpy.AddMessage(sRows3) # diagnostics 2

                with arcpy.da.UpdateCursor(outDBFTable,("OBJECTID",fieldList[counter2])) as uCursor1:
                    arcpy.AddMessage(uCursor1)# diagnostic 3
                    for uRows1 in uCursor1:
                        arcpy.AddMessage(uRows1) # diagnostics 4
                        if sRows3[0] == uRows1[0]:
                            arcpy.AddMessage("true true") # diagnostics 5
                            uCursor1.updateRow([sRows3[1]]) # <- Error raised here
                        else:
                            arcpy.AddMessage("false false") # diagnostics 6

        counter2 =+ 1

del (sCursor2)
del (sCursor3)
del (uCursor1)

...

 

Every time sCursor2 performs its task and produces a new rasTable, I want to use the "Count" column <number> from rasTable to update outDBFTable fields where outDBFTable "OBJECTID" == rasTable "OBJECTID". The counter is in there to position the cursor over the correct field from fieldList (field1, field2...) every time a new rasTable is produced. Mechanically, I'm confident all of this works because if I comment out  "uCursor1.updateRow([sRows3[1]])", the printout in the ArcGIS Pro tool message dialogue box behaves just as I expect. Or based on the error, I'm overly confident...

Basically, I don't understand why I'm getting a sequence size error when the diagnostics show the rows to be the same size.

I've attached a image of the error dialogue. 

Please let me know if more information is needed. 

 

Any help would be great!

0 Kudos
14 Replies
AdamG2
by
New Contributor III

Well I guess this really shows my true experience with Python... (...pretty low). I had originally wanted to use Zonal Statistics because I thought it would be the right tool, but I wasn't able to get it to work the way I wanted. 

DanPatterson
MVP Esteemed Contributor

counters align with the nearest 'for' not the 'while'

either indent or dedent your counter as appropriate


... sort of retired...
AdamG2
by
New Contributor III

Hi DanPatterson,

Thank you for your comments. 

Not sure if you get notifications when your handle is mentioned, so I wanted to notify you about what I should do for accepting the solution. If you go check out my reply to @Anonymous User's post, you'll see what I mean.

 

Thanks 

0 Kudos
DanPatterson
MVP Esteemed Contributor

counters are always +=, -=, /=  etc with the = sign on the right.

c = c + 1  makes a new object

c += 1  changes an existing object in place

The reason I question the location of the counter is because "with" is a context operator and I wanted to check to see if you wanted to increate the counter within a "for" loop or after it is done.

As for the answers, you can check as many as you feel helped get to your solution, so all is good.


... sort of retired...
AdamG2
by
New Contributor III

Great, thank you for that clarification. Also I didn't realize I could mark several solutions. Great feature.

 

As for the counter, I wanted it to increase with every iteration of the highest level "for loop". It accomplishes that where it is. Thanks for all your help!

0 Kudos