1

I don't want to write an if statement for every field. I want to know if there is another way to field calculate each field with the values related to it in this example table:

enter image description here

Fields (X,Y,INSTALLDATE,BLDGID,LIFECYCLE,FACILITYID,REGION)

Values (0,0,1/1/9999,NA,TBD,NA,NA)

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = Database Path
Asset = "Asset"
try:
    for fc in arcpy.ListFeatureClasses("ONUS_ssManhole") + arcpy.ListFeatureClasses("ONUS_ssInterceptor") + arcpy.ListFeatureClasses("ONUS_ssHoldingTank") + arcpy.ListFeatureClasses("ONUS_ssCleanOut") + arcpy.ListFeatureClasses("ONUS_ssFitting") + arcpy.ListFeatureClasses("ONUS_ssLiftStation") + arcpy.ListFeatureClasses("ONUS_ssValve") + arcpy.ListFeatureClasses("ONUS_ssPressurizedMain"):
        fcs = fc
        arcpy.MakeFeatureLayer_management(fc, Asset)
        for field in arcpy.ListFields(fcs, "X") + arcpy.ListFields(fcs, "Y") +  arcpy.ListFields(fcs, "INSTALLDATE") +  arcpy.ListFields(fcs, "LIFECYCLE") + arcpy.ListFields(fcs, "PROJECTID_ADD") +   arcpy.ListFields(fcs, "FACILITYID"):
            fields = field
            expnull = ("{0} is NULL").format(field.name)
            arcpy.SelectLayerByAttribute_management(Asset, "NEW_SELECTION", expnull)
            result = arcpy.GetCount_management(Asset)
            count = int(result.getOutput(0))
            if count == 0:
                print(fc + " {0} is Good").format(field.name)
            if count > 0:
                print (count)
                arcpy.CalculateField_management(Asset,(????????)
                print(fc + " {0} is now clear of NULL Values").format(field.name)
except Exception:
    e = sys.exc_info()[1]
    print(e.args[0])
4
  • So you want to apply the "default value" in your table to the field when you find rows for that field equal to NULL? Is it possible in your data to say have NULL X's but for the same row have populated Region fields? What I am getting at is when you have a null in X are all other fields for that row NULL by default? – Hornbydd Dec 23 '19 at 19:26
  • No, All other Fields are not NULL by default. – Tony Dec 23 '19 at 19:54
  • 1
    In that case, as you expected, a set of IF statements is required as you are applying a different value to a different field for a set of different rows. – Hornbydd Dec 23 '19 at 20:38
  • 1
    An if-elif-else cascade isn't the only way to handle this; you could also use a dictionary to map conditional responses. – Vince May 11 '20 at 23:25
3

I would think an arcpy.da.UpdateCursor would be a better way to go at this (much more efficient). You can load the fields of that row into the cursor and do whatever logic you want to populate your field, and update, row by row.

If you need to look at other data and aggregate it, sometimes you can use a SearchCursor to go through a table and populate a dictionary, then use an update cursor to plant values on a second pass.

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