Last active
January 31, 2016 08:10
-
-
Save AlexArcPy/57f3d53820cbb1ce1695 to your computer and use it in GitHub Desktop.
Sample arcpy helper function - get not null fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcpy | |
def get_fields(input_fc,only_field_type="String",not_null=True): | |
'''returns list of field names of specified data type and contain at | |
least one not NULL value''' | |
if not not_null: | |
all_fields = [field.name for field in arcpy.ListFields(in_fc,field_type=only_field_type)] | |
return all_fields | |
else: | |
all_fields = [field.name for field in arcpy.ListFields(in_fc,field_type=only_field_type) | |
if field.isNullable != "False"] | |
#getting a dict {field name : [list of all values]} | |
fields_dict = {field: list(set([feature[all_fields.index(field)] | |
for feature in arcpy.da.SearchCursor(in_fc,all_fields)])) | |
for field in all_fields} | |
null_fields = [k for k,v in fields_dict.iteritems() if v == [None]] | |
not_null_fields = list(set(all_fields).symmetric_difference(set(null_fields))) | |
return not_null_fields |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment