Unselect features from arcpy.GetCount_management

1157
8
Jump to solution
05-31-2017 11:30 AM
CCWeedcontrol
Regular Contributor

I have a script that runs in arcmap Python window. It does a select layer by location then exports the selected features but i need to remove features with Null or empty attributes in a specific filed before acpy.TableToTable but some times there will be none with Null or empty attributes. I am not sure how to do that so if someone could show me how to this it would be awesome.

Currently this is the code i  have.

TA = "TAXLOTS"
SP4 = "SUBJECT_PROPERTY"

arcpy.SelectLayerByLocation_management(TA, "WITHIN_A_DISTANCE", SP4, "600 Feet", "NEW_SELECTION")

try:
    if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
        arcpy.TableToTable_conversion("TAXLOTS", wp, "NOTIFIED_LOTS.dbf")

    arcpy.SelectLayerByAttribute_management("TAXLOTS", "CLEAR_SELECTION")
except:
    pass‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

You remove features from a selection using the same tool as you use to add them:

Select Layer By Attribute—Help | ArcGIS for Desktop 

or

Select Layer By Location—Help | ArcGIS for Desktop 

Check the 'selection_type' parameter, 'REMOVE_FROM_SELECTION' option.

View solution in original post

8 Replies
JoshuaBixby
MVP Esteemed Contributor

How many fields are in this layer?  Do you want to exclude records if a certain field or two has a NULL or empty, or do you want to drop a record if any field has a NULL or empty?

0 Kudos
CCWeedcontrol
Regular Contributor

There is about 10 fields in the layer. Of the selected I would like to exclude records if a certain field is NULL or empty .

0 Kudos
DarrenWiens2
MVP Honored Contributor

You remove features from a selection using the same tool as you use to add them:

Select Layer By Attribute—Help | ArcGIS for Desktop 

or

Select Layer By Location—Help | ArcGIS for Desktop 

Check the 'selection_type' parameter, 'REMOVE_FROM_SELECTION' option.

CCWeedcontrol
Regular Contributor

I figured i was over looking something simple.

Will line 2 this handle NULL's ?

if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
        arcpy.SelectLayerByAttribute_management("TAXLOTS", "REMOVE_FROM_SELECTION", "\"DXF_TEXT\" = ' '")

0 Kudos
curtvprice
MVP Esteemed Contributor

Will line 2 this handle NULL's ?

No, you need to handle both cases - blank and null.

if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
    arcpy.SelectLayerByAttribute_management(
        "TAXLOTS",
        "REMOVE_FROM_SELECTION",
        "DXF_TEXT = ' ' OR DXF_TEXT IS NULL")‍‍‍‍‍‍‍‍‍‍‍‍
CCWeedcontrol
Regular Contributor
With i run the following in ArcMap python window  
if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:
    arcpy.SelectLayerByAttribute_management("DXF_TEXT = ' ' OR DXF_TEXT IS NULL" 
I get error,
ERROR 000732: Selecting Features: Dataset DXF_TEXT = '' OR DXF_TEXT IS NULL does not exist or is not supported
Failed to execute (SelectLayerByLocation).


The blank and NULL records don't get removed.

0 Kudos
curtvprice
MVP Esteemed Contributor

You are missing the first two arguments for the Select Layer By Attribute tool.

0 Kudos
CCWeedcontrol
Regular Contributor

oh my bad i had 'Select by Location' instead of 'Select layer by attribute'.

0 Kudos