0

I have a File Geodatabase Table(no geometry). I am trying to update the last column, called S_Name from concatenated values from other fields. I am going to write out my logic so it is clear what I want to accomplish

if the PRE_TYPE field is not Null and the SUF_TYPE field is null then update S_Name to the values of PRE_TYPE+NAME+SUF_MOD

if PRE_TYPE is Null and SUF_TYPE is Not Null then update S_Name to NAME+SUF_TYPE

enter image description here

Here is my code below. I am pretty sure the logic is sound. The script runs with no errors but does not print bah, and does not update the table.

import arcpy
arcpy.env.workspace = "C:path\\Tran_road_NJ.gdb"
layer = "C:path\\Tran_road_NJ.gdb\\Tran_road_geocode_NJ_1"
with arcpy.da.UpdateCursor(layer, ['PRE_TYPE','NAME','SUF_TYPE','SUF_MOD','S_Name']) as rd:
    for x in rd:
        if x[0] != None and x[1] != None and x[2] == None:
            bah = x[0]+' '+x[1] +' '+x[3]
            x[4] = str(bah)
            print bah
            rd.updateRow(x)
        elif x[0] == None and x[1] != None and x[2] != None:
            bah = x[1]+' '+x[2]
            x[4] = str(bah)
            print bah
            rd.updateRow(x)
  • The cells with no values are probably just empty strings and not None. Check the value in them and try x[0] != "" or " " – Brian Sep 19 '16 at 15:57
1

As @Brian mentioned in his comment, the table most likely contains empty strings rather than nulls. Try this code:

import arcpy
arcpy.env.workspace = "C:path\\Tran_road_NJ.gdb"
layer = "C:path\\Tran_road_NJ.gdb\\Tran_road_geocode_NJ_1"
with arcpy.da.UpdateCursor(layer, ['PRE_TYPE','NAME','SUF_TYPE','SUF_MOD','S_Name']) as rd:
    for x in rd:
        if all([x[0], x[1]]) and (x[2] == None or not str(x[2]).strip()):
            bah = ' '.join(x[:2] + [x[3]])
            x[4] = bah
            print bah
            rd.updateRow(x)
        elif all([x[1], x[2]]) and (x[0] == None or not str(x[0]).strip()):
            bah = ' '.join(x[1:3])
            x[4] = bah
            print bah
            rd.updateRow(x)
  • very nice, I have never seen the all function. It just checks whether or not the items places in the function exist? – ziggy Sep 19 '16 at 16:22
  • 2
    The all() function will evaluate that all conditions in a list are True. So basically like using and. On the contrary, the any() function evaluates if one or more conditions are True. Empty strings and NoneTypes can be considered falsy values, so I'm not explicitly testing. – crmackey Sep 19 '16 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.