Slicing fields in arcpy.da.UpdateCursor

879
5
Jump to solution
05-17-2018 12:38 PM
by Anonymous User
Not applicable

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)
Tags (1)
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

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

View solution in original post

0 Kudos
5 Replies
NeilAyres
MVP Alum

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.

DanPatterson_Retired
MVP Emeritus

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
0 Kudos
JoeBorgione
MVP Emeritus

The ol' none versus '' versus ' ' versus '   '  issue....

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Emeritus

But there is only one None

0 Kudos
JoeBorgione
MVP Emeritus

Which is why I'm such a fan....

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