I wrote a function such that if the passed val
only has whitespaces in it for example ' '
, they all get removed and we return np.Nan
or a null
value.
def check_for_empty_spaces(val):
val = val.rstrip()
if len(val)<1:
return np.NaN
else:
return val
print(len(check_for_empty_strings('WHAJS ')))
print(check_for_empty_strings('WHAJS'))
print(check_for_empty_strings(' '))
print(check_for_empty_strings(''))
The function returns as desired. The output is:
5
WHAJS
nan
nan
but now when I use this function, I want to check that the string is
- not just whitespaces
- not NULL
However, when I check this:
check = check_for_empty_strings('')
if (check):
print('tru')
print(check)
else:
print('dsj')
I get this output
tru
nan
Why is the first one tru? If check == NaN then shouldn't if (check)
be False? How else can I check that my value is not just whitespaces or NULL.
CodePudding user response:
Well you can check for np.nan
:
np.isnan(check)
Or you can return None
instead of np.nan
and then check for it as you have done above in the if condition because bool(None)
evaluates to False
. If you evaluate bool(np.nan)
on the other hand you get the output to be True
.
CodePudding user response:
check = check_for_empty_strings('')
if (np.isnan(check)):
print('tru')
print(check)
else:
print('dsj')
This works