Select to view content in your preferred language

UpdateCursor error: fields size must match size of the row

668
5
Jump to solution
07-20-2024 04:23 PM
JustinMuldowney
New Contributor

Using Pyscripter to take the last 5 characters (the ZIP code) of an address field and putting that into a separate field.

I am getting the following error: 

File "C:\Users\battl\Desktop\experiment.py", line 21, in <module>
    zipCursor.updateRow([zipRow])
TypeError: fields size must match size of the row
 

When I google it, it's saying that the zipRow at the end needs to be designated as a list (thus the square brackets, it doesn't work with or without them)

Here's my code:

import arcpy

arcpy.env.workspace = r"C:\GIS\VoteTally"
gdbTable = "\\Voters.gdb\\voterRecords"
 
addressField = "RESIDENTIAL_ADDRESS__CITY_STATE_ZIP"
zipField = "ZIP"
 
with arcpy.da.UpdateCursor (gdbTable, (addressField, zipField)) as zipCursor:
    for zipRow in zipCursor:
        street = zipRow[0]
        zipCode = street[-5:]
        zipRow = zipCode
        zipCursor.updateRow([zipRow])
 
Any help would be greatly appreciated.
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

You're reading in two fields and returning (updating) one.You need to pass the same number and order of fields back to updateRow, even if you're not actually changing all of them.

 

with arcpy.da.UpdateCursor (gdbTable, (addressField, zipField)) as zipCursor:
    for zipRow in zipCursor:
        street = zipRow[0]
        zipCode = street[-5:]
        zipRow = [street, zipCode]
        zipCursor.updateRow(zipRow)

 

 

View solution in original post

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor
0 Kudos
Luke_Pinner
MVP Regular Contributor

You're reading in two fields and returning (updating) one.You need to pass the same number and order of fields back to updateRow, even if you're not actually changing all of them.

 

with arcpy.da.UpdateCursor (gdbTable, (addressField, zipField)) as zipCursor:
    for zipRow in zipCursor:
        street = zipRow[0]
        zipCode = street[-5:]
        zipRow = [street, zipCode]
        zipCursor.updateRow(zipRow)

 

 

0 Kudos
HaydenWelch
Frequent Contributor

To add to this, always declare your field list/tuple as a single variable. If you are doing read and update operations and have to change a field it can become a nightmare if you forget to update the field order or field names somewhere down the line.

 

**cursor_fields = (addressField, zipField)**

 

Then use that anywhere that you need those specific fields accessed.

0 Kudos
JustinMuldowney
New Contributor

That fixed the error I was seeing. It was then saying that street[-5:] was a None Type. When the table is created, it randomly added a completely Null row at 27096 (there's 140k). So it was getting tripped up there. I added a If row[0] is None delete row and it now works.
I also had to edit your line 5 to zipRow[1] = zipCode. The way you had it basically just copied the full street address into the zip field.

Thank you for the assist!

0 Kudos
Luke_Pinner
MVP Regular Contributor

@JustinMuldowney wrote:

...

I also had to edit your line 5 to zipRow[1] = zipCode. The way you had it basically just copied the full street address into the zip field.

No, that would do exactly the same as what I wrote, it would return the original street address in the first element and the zip code in the second.

e.g.

 

zipRow = ["abc 12345", None]  # Some dummy data
street = zipRow[0]
zipCode = street[-5:]
zipRow[1] = zipCode 
print(zipRow == [street, zipCode])

True

 

 

 

0 Kudos