Sequence Size error in Update Cursor

687
3
Jump to solution
02-27-2019 03:12 PM
JoeBorgione
MVP Emeritus

I have this reoccurring nightmare... (see 'ascii' codec can't encode character u'\u201c'  )  I keep coming back to this script where I need to replace a new line ('\n') in a field.  I have somehow gotten past the error in the referenced thread, but now I'm faced with 

TypeError: sequence size must match size of the row

import arcpy
arcpy.env.workspace = r'J:\WaterQuality\test_tables.gdb'

table = 'FieldParameters'
fieldNames = [f.name for f in arcpy.ListFields(table,'','String')]
for fieldName in fieldNames:
   field = [fieldName]
    with arcpy.da.UpdateCursor(table,field) as cursor:
      for row in cursor:
        if row[0] == None:
          pass
        elif '\n' in row[0]:
          print(row[0])
          row[0] = row[0].replace('\n','=>')
          print(row[0])
          cursor.updateRow(row[0])
            
Water has a green/grey tint


Water has a green/grey tint=>=>

Line 18 through line 20 are returned from the print statement on line 13.  The actual value of the field is:

Water has a gren/grey tint\n\n

Line 21 is returned from the print statement on line 15: I have successfully replaced both \n characters with =>

However, it errors with:

Runtime error
Traceback (most recent call last):
File "<string>", line 23, in <module>  (line 23 is shown as line 16 above)
TypeError: sequence size must match size of the row

This table has 11 string fields, the first 9 do not have any \n characters in any of them.  I hit the 10th field and the first record it finds with a \n tosses the error.  

This is being run in the Arcpy window of ArcMap 10.6.1. I've tossed this error before with an insert cursor (see: Brain cramp with an Insert Cursor from 9 months ago) and I was able to resolve it then.  I just don't see what I'm doing wrong this time.

My goal is to loop through a list of tables and their respective string fields looking for \n characters and replace them accordingly.  In a  search cursor and find all the offenders just fine, but fixing them is another story.

That should just about do it....
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

don't you want to update the row?

...
 row[1] = row[0] * 100
 cursor.updateRow(row)
...

Accessing data using cursors—ArcPy Get Started | ArcGIS Desktop 

that may be why your sequence size is incorrect

View solution in original post

3 Replies
DanPatterson_Retired
MVP Emeritus

Don't know about the loopy stuff, but just want to make sure that you are looking for the right thing

EDIT

Added a 'check_for'

def stuff_test(i, check_for=r"\n"):
    if i is None:
        print("None type")
        return str(i)
    if check_for in i:
         print(f'is in, {i}')
    else:
         print(f'not in {i}')
    return i
    

# ---- stuff to check

stuff = ['n', '\\n', 'a\n', None, r'b\n', r'c\n']

# ---- not raw encoded

back = [stuff_test(i, "\n") for i in stuff]
not in n
not in \n
is in, a

None type
not in b\n
not in c\n

# ---- raw encoded
back = [stuff_test(i, r"\n") for i in stuff]
not in n
is in, \n
not in a

None type
is in, b\n
is in, c\n
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DanPatterson_Retired
MVP Emeritus

don't you want to update the row?

...
 row[1] = row[0] * 100
 cursor.updateRow(row)
...

Accessing data using cursors—ArcPy Get Started | ArcGIS Desktop 

that may be why your sequence size is incorrect

JoeBorgione
MVP Emeritus

Just what I needed: another pair of eyes!  Thanks Dan.  No more nightmares. Until the next one...

That should just about do it....
0 Kudos