1

I'm working with an attribute table in ArcMap 10.2 where there are some 30 fields mainly consisting of NULL values. I don't want to display to users a table full of NULL values as it'll be difficult to read. See below image:

enter image description here

So the result I'd like to create is a Field called SUMMARY that includes the "field name: Value. field name: Value" of any row that is NOT NULL e.g. A: T. D: 10/01/2011

In this way a user can select a feature with the identify tool and see what values there are rather than scrolling through loads of NULL values.

Does anyone have suggestion on the general construct of the script? Including the da.SearchCursor, da.UpdateCursor, and I guess by way of row index if statements to query if not none add to SUMMARY field with delimators ":" after field name and "." after row value.

1
  • 1
    For this you should only need to use an arcpy.da.UpdateCursor and there are some techniques for handling nulls in gis.stackexchange.com/questions/110309/…. I recommend that you first try to write a script that just writes say "XXX" to SUMMARY when it encounters a <Null> in one specific field. Once that works the remainder should follow.
    – PolyGeo
    Commented Sep 10, 2014 at 10:22

1 Answer 1

2

Here is some untested code that should do this. It assumes you want to skip over the first field which is ObjectID and that the summary field is the last field in your table. All other fields in between would be evaluated and included in the summary, although it should include logic to skip over the shape field or any GlobalID field. Of course the SUMMARY field needs to have at least as many characters in the field as the longest concatenation of field names and values possible to avoid a potential error of a string overflow:

import arcpy  

updateFC = r"C:\workspacePath\featureClass"  

updateFieldsList = arcpy.ListFields(updateFC)  

with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:    
    for updateRow in updateRows:
        strSummary = ""    
        for n in range (1,len(updateFieldsList) - 1):      
            if updateFieldsList[n] <> "Shape" and updateRow[n] <> none:
                if strSummary > "":
                    strSummary = strSummary + ", "
                strSummary = strSummary + updateFieldsList[n] + ": " + str(updateRow[n])
        updateRow[n+1] = strSummary
        updateRows.updateRow(updateRow)

The above should work for file/personal geodatabases and shapefiles (although shapefiles do not support text fields with more than 255 characters, so they probably won't work for this particular data). If this data is in a versioned SDE database or in a complex feature class with things like topology, feature-linked annotation, geometric network, etc. you will need to set up an Editor session and operation around the update cursor code.

However, if you set up an Editor session on a Feature Class with feature linked annotation and include any field that participates in an annotation label expression or label query definition in the update cursor field list those fields will be treated as modified fields by the Editor even if the values were not changed and the annotation will be rebuilt by the label engine and destroy any manual edits. This is a behavior I have reported to Esri and I am requesting that it be classified as a bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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