I am trying to perform an operation on a string field with a condition that it contains data. Referencing None with Python does not appear to work when the field is nullable and contains <Null> for its data. Am I missing something? I have tried len() and "<Null>" and "Null" as well. What do?

The code, for kicks:

def apt(addr,aptnum):
    if aptnum != None:
        a = addr.split(aptnum,1)[-1]
        return a
        return addr 

Update: It would appear that my problem is that field calculations are not processing on any rows where a null column is tested. That is outside the scope of this question, however, so I will leave it for the time being.

  • Have you compared it to the null string ""?
    – whuber
    Aug 9 '11 at 21:26
  • Are you on the latest service pack? Aug 9 '11 at 23:17
  • @Jason Scheirer No, we have not upgraded to SP2 yet.
    – Nathanus
    Aug 10 '11 at 15:24
  • @Nathanus Ok, I just recently did a bunch of fixes related to NULLs in field calculator and I'm pretty sure they made it into SP2. Aug 10 '11 at 16:40
  • I have the same problem. I am trying to remplace <Null> in a field with 0. I add another field and then I use the codeblock. But nothing is happend. codeblock: """def reclass ( Neighbours2): if Neighbours2 == None: return 0""" Expression: "reclass (!Neighbours2!)" Thanks for the help.
    – user6173
    Mar 1 '12 at 9:40

I don't know what you are querying (eg shapefile, geodatabase), but have a look at http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s50000002t000000.htm specifically the section on "The NULL keyword"

EDIT Adding a field to a shapefile, using the field calculator on this field, then using the following as the code block (Python parser)

def TestForNull(a_field):
  if not a_field:
   return "is null"

with TestForNull(!YourFieldNameHere!) in the expression line, results in "is null" being added to the field. You will of course have to modify to suit your purposes.

  • This works in the field calculator Codeblock? I'm a bit lost as to how this will help me out.
    – Nathanus
    Aug 9 '11 at 22:43
  • 1
    did you try it? ie "myFieldNameHere" IS NULL where of course the field is your field. In theory, it should select the records with Null values. I don't have any shapefiles that have null records, so you will have to test it on your own data. Check the other options if you don't want the null records.
    – user681
    Aug 9 '11 at 23:13
  • SQL syntax does not work on the Field Calculator dialog (unless you are using a cursor in the code block which is not a very good idea).
    – blah238
    Aug 9 '11 at 23:33
  • @blah Dan Patternson is not using SQL syntax. His solution is pure Python.
    – whuber
    Aug 10 '11 at 13:11
  • This works well enough. Apparently my problem is that the field itself is not responding to field calculations. Joy.
    – Nathanus
    Aug 10 '11 at 16:11

Try just

if aptnum:

This tests for both null and zero-length strings.


or you could use if aptnum is None:


This answer from PolyGeo worked for me: https://gis.stackexchange.com/a/81155/44980

.strip() removes all whitespace characters

def TestForNull(Field1):
  if Field1.strip() == "":
    return 'is null'
    return 'not null'
  • This should be the accepted answer. I spent a lot of time figuring out that an empty text field is actually " " instead of "".
    – pbreach
    Mar 7 '16 at 4:05

Try calling the codeblock as apt(!addr!,str(!aptnum!)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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