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.
5 Answers 5
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– Nathanus2011年08月09日 22:43:54 +00:00Commented Aug 9, 2011 at 22:43
-
1did 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– user6812011年08月09日 23:13:29 +00:00Commented 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).blah238– blah2382011年08月09日 23:33:37 +00:00Commented Aug 9, 2011 at 23:33
-
@blah Dan Patternson is not using SQL syntax. His solution is pure Python.whuber– whuber2011年08月10日 13:11:20 +00:00Commented 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.Nathanus– Nathanus2011年08月10日 16:11:12 +00:00Commented Aug 10, 2011 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'
else:
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– pbreach2016年03月07日 04:05:14 +00:00Commented Mar 7, 2016 at 4:05
Try calling the codeblock as apt(!addr!,str(!aptnum!)).
Explore related questions
See similar questions with these tags.
NULL
s in field calculator and I'm pretty sure they made it into SP2.