Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
    else:
        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.

share|improve this question
    
Have you compared it to the null string ""? –  whuber Aug 9 '11 at 21:26
    
Are you on the latest service pack? –  Jason Scheirer 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. –  Jason Scheirer 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. –  Angela Blanco Mar 1 '12 at 9:40

4 Answers 4

up vote 6 down vote accepted

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.

share|improve this answer
    
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. –  Dan Patterson 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.

share|improve this answer

or you could use if aptnum is None:

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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