I am trying to turn off all fields in a MXD that have all null values.

I have been looking at a SearchCursor without much luck.

  • I don't understand what "turn off" a field means or why you would think you should use a SearchCursor to "turn off" a field. I think you really mean that you want to filter out all features/records that have all null values. You can do that with a definition query on a layer or a query that applies to a SearchCursor. That would be done usng SQL like: NOT (FIELD1 IS NULL AND FIELD2 IS NULL) – Richard Fairhurst Mar 7 '16 at 18:01

The approach I would take is create a list of all fields in the featureclass. Start a SearchCursor, and check all fields for a non-null value. If a non-null value is found, remove that field from the list.

    nullFields = [y.name for y in arcpy.ListFields(fc)]
    for row in arcpy.SearchCursor(fc):
        for x in nullFields[:]: #must use a copy of nullFields here
            if not row.isNull(x):

The remaining items in nullfields will have all null values.

| improve this answer | |
  • Sorry, I do not want to remove from the list. I want to turn off a field that has all null values in all the layers in a MXD. – Dan MacDonald Mar 7 '16 at 18:12
  • The code above determines all of the fields in a feature class that contains all null values. You can add whatever code you need to 'turn off' the fields in the resulting list, and wrap it all in a loop that will examine all of the layers in the MXD. – Bjorn Mar 7 '16 at 18:18
  • 3
    If you want to make a field invisible you need to use the FieldInfo properties interface and use the SetVisible method to change the visibility of a field index to False. You can find the field index from the Field Name using the findFieldByName method. help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… – Richard Fairhurst Mar 7 '16 at 18:30
  • - 1 because a) OP doesn't want to remove fields and b) you don't iterate through all records to check if they are all Null – FelixIP Mar 7 '16 at 21:26
  • 1
    Ok, missed it my bad. Still very slack way. Where is : after [:] ? Shouldn't it be isNull(x.name) ? – FelixIP Mar 8 '16 at 1:27

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