0

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.

closed as off-topic by PolyGeo Mar 8 '16 at 11:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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
2

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):
                nullFields.remove(x)

The remaining items in nullfields will have all null values.

  • 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.