Testing for empty fields

2065
5
09-05-2011 02:48 PM
LeighHolcombe
New Contributor
Hello,
I have a Python script which flags records with inappropriate values.  Unfortunately, it is also flagging records without values.  I would like to know how I can check for the existence of a value in a field.  Normally, if there is a value in this field, it would be a string.  Here's what I have currently:
if (suffval != '') and (suffval not in allowed):

the first condition is the one I'm having trouble with - the second condition seems to work fine.

I have also tried:
if (suffval is not None)


and:
if (not len(suffval))


Thanks so much for any guidance you can provide.
Tags (2)
0 Kudos
5 Replies
StacyRendall1
Occasional Contributor III
By "records without values" do you mean records with NULL or records with just an empty string? If a string is empty it will have length zero.

I think this should do the trick:
"if not allowed and not none and not an empty string" (well, maybe you don't have to worry about the None, so you can delete that perhaps)
if (suffval not in allowed) & (suffval is not None) & (len(suffval) != 0):
0 Kudos
LeighHolcombe
New Contributor
By "records without values" do you mean records with NULL or records with just an empty string? If a string is empty it will have length zero.


I assume the fields in question are NULL.  I'm working with a street database, and this particular field is the directional suffix, which not every street requires.
0 Kudos
StacyRendall1
Occasional Contributor III
Arc NULL is None in Python. So
if (suffval not in allowed) & (suffval is not None) & (len(suffval) != 0):
    # flag record

will flag records where the suffval is not allowed, not NULL (None) and not empty string.
0 Kudos
LeighHolcombe
New Contributor
When your suggested code didn't work, I prayed something else was wrong.  Sure enough, instead of using NULLs or empty strings, my working database used a whitespace character.  The following code produced the intended results:
if (suffval != ' ') and (suffval not in allowed)

(that's a space between the single quotes)

Thanks for your guidance, problem solved.
0 Kudos
StacyRendall1
Occasional Contributor III
Sweet, another handy one is isspace, which will pick up multiple spaces as well (your current code will only pick up one space). I.e.:
if (not suffval.isspace()) and (suffval not in allowed):
0 Kudos