6

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.

asked Aug 9, 2011 at 20:59
5
  • Have you compared it to the null string ""? Commented Aug 9, 2011 at 21:26
  • Are you on the latest service pack? Commented Aug 9, 2011 at 23:17
  • @Jason Scheirer No, we have not upgraded to SP2 yet. Commented Aug 10, 2011 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. Commented Aug 10, 2011 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. Commented Mar 1, 2012 at 9:40

5 Answers 5

6

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.

answered Aug 9, 2011 at 21:44
6
  • This works in the field calculator Codeblock? I'm a bit lost as to how this will help me out. Commented Aug 9, 2011 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. Commented Aug 9, 2011 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). Commented Aug 9, 2011 at 23:33
  • @blah Dan Patternson is not using SQL syntax. His solution is pure Python. Commented Aug 10, 2011 at 13:11
  • This works well enough. Apparently my problem is that the field itself is not responding to field calculations. Joy. Commented Aug 10, 2011 at 16:11
3

Try just

if aptnum:

This tests for both null and zero-length strings.

answered Aug 9, 2011 at 23:34
1

or you could use if aptnum is None:

answered Aug 10, 2011 at 7:13
1

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'
 else:
 return 'not null'
answered Mar 17, 2015 at 15:09
1
  • This should be the accepted answer. I spent a lot of time figuring out that an empty text field is actually " " instead of "". Commented Mar 7, 2016 at 4:05
0

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

answered Mar 1, 2012 at 15:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.