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:
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
Solved! Go to Solution.
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)
related error with solutions
Solved: sequence size must match size of the row - Esri Community
Solved: UpdateCursor: "TypeError: sequence size must match... - Esri Community
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)
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.
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!
@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