Arcpy - Find data that is not NULL

9054
4
Jump to solution
08-19-2014 10:01 AM
AliciaSoto
Occasional Contributor

I have a shapefile with an attribute table similar to the one below.

NAME
CATDOG
***Johnblackblack
***Pattabby<Null>
Joe<Null>white

First, I want to search for any rows where 'NAME' starts with '***'. Then, from that selection I want to print out data if the 'DOG' column is not <Null>.

Using the table above the output should look like this.

C:\MyDocs\Python>python Animal.py

***John

black

C:\MyDocs\Python>

Here is my code so far

def findAnimal(shapefilePath):

  fields = ('NAME','CAT','DOG')

  cursor = arcpy.da.UpdateCursor(sp,fields)

  for row in cursor:

       nameStr = row[0]

       if nameStr[0:3] == "***":

            if row[2] != None:

                 print row[0]

                 print row[2]

       cursor.updateRow(row)

This is what my code is currently printing out.

C:\MyDocs\Python>python Animal.py

***John

black

***Pat

C:\MyDocs\Python>

There is blank row after '***Pat' ,so it looks like it is printing out the <Null> value.

It appears that Line 07 in my code is not working.

Tags (4)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

I tried to reproduce this and created a shapefile with 3 points and added the attributes as displayed in your example (leaving two fields untouched). You visualize them as <Null>, but a DBF does not have <Null> values as in a geodatabase table.

I change the code to show what it actually prints and it seems to be a single space... When testing against a single space it detects the "<Null>" values.

import arcpy

def findAnimal(shp):

  fields = ('NAME','CAT','DOG')

  cursor = arcpy.da.UpdateCursor(shp,fields)

  for row in cursor:

      nameStr = row[0]

      if nameStr[0:3] == "***":

            if row[2] != " ":

                print row[0]

                print "'{0}'".format(row[2])

      cursor.updateRow(row)

fc = r"C:\Forum\CatsDogs\CatsDogs.shp"

findAnimal(fc)

Kind regards, Xander

View solution in original post

4 Replies
IanMurray
Frequent Contributor

Hi Alicia,

ArcMap returns a string None for null value in a table instead of a None object.  Change line 7 to if row[2] != "None" and it should work right.

If you are on 10.0 or older, then your method would work, since it return an empty string, see this topic

40913 - How to check for null values in the Field Calculator using Python

0 Kudos
AliciaSoto
Occasional Contributor

Hmmmm it's still doing the same thing.

I am using version 10.2

0 Kudos
XanderBakker
Esri Esteemed Contributor

I tried to reproduce this and created a shapefile with 3 points and added the attributes as displayed in your example (leaving two fields untouched). You visualize them as <Null>, but a DBF does not have <Null> values as in a geodatabase table.

I change the code to show what it actually prints and it seems to be a single space... When testing against a single space it detects the "<Null>" values.

import arcpy

def findAnimal(shp):

  fields = ('NAME','CAT','DOG')

  cursor = arcpy.da.UpdateCursor(shp,fields)

  for row in cursor:

      nameStr = row[0]

      if nameStr[0:3] == "***":

            if row[2] != " ":

                print row[0]

                print "'{0}'".format(row[2])

      cursor.updateRow(row)

fc = r"C:\Forum\CatsDogs\CatsDogs.shp"

findAnimal(fc)

Kind regards, Xander

AliciaSoto
Occasional Contributor

Ah! That's what it was! Thanks!

0 Kudos