I am trying to join multiple fields using an update cursor, I want to trim a filed to a specific length while the cursor iterates through and adds another field to the final string. I am working with a streets data set and will need to do this for streets that have no pre direction and streets that have a pre direction. I want to take only the first 5 character of the street name when there is no pre directions and the first 3 character when there is a pre direction.
When I run this script (testing it only for streets with no pre direction) I do not get any errors but nothing is updated in the feature class.
import arcpy fc = r'C:\Users\achapman\Documents\18_0157_Woodlands_TX\WOODLANDS_TX.gdb\WOODLANDS_CL_WGS84' pbid = ['PRE_DIR', 'ST_NAME', 'ST_TYPE', 'pBID'] with arcpy.da.UpdateCursor(fc,pbid) as pbid_cursor: for row in pbid_cursor: if (row [0] is None): row[3] = (row[1][:5]) + row[2] pbid_cursor.updateRow(row)
Solved! Go to Solution.
To follow on from Neil's comment, you have to check for what you think is None. like a space, an empty string, a real None and anything else you can think of.
Print statements would help if you are running this script stand-alone
if row[0] in (None, "", " "): # check for None (aka <null>, empty string, space
You might want to take out that space -
if (row [0] is None)
But what is the data type of "PRE_DIR". Are you sure it can be a None type?
Even an empty string like "" will make it True.
To follow on from Neil's comment, you have to check for what you think is None. like a space, an empty string, a real None and anything else you can think of.
Print statements would help if you are running this script stand-alone
if row[0] in (None, "", " "): # check for None (aka <null>, empty string, space
The ol' none versus '' versus ' ' versus ' ' issue....
But there is only one None
Which is why I'm such a fan....