Remove Empty Spaces with Field Calculator

1894
3
Jump to solution
08-01-2017 09:18 AM
NickArcher
New Contributor

Hi,

I have a fields that is populated with correct values, <Null>, and whitespace/empty. The field is of text value with both numbers and letters in the correct values. I want to remove all of the whitespace/empty and replace them with <Null> instead. I am trying to do this with the field calculator and python.

My current prelogic statement is:

def makeNull(unitType):
  if unitType == str():
    return None

And the box below is: 

makeNull( !UnitType!)

However when the field is calculated it turns all of the values to <Null> instead of just the whitespace/empty. Any Idea what I am doing wrong?

0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor

Maybe you only try evaluating empty strings.

def makeNull(unitType):
  if unitType == "":
    return None
  else:
    return unitType‍‍‍‍‍

View solution in original post

3 Replies
IanMurray
Frequent Contributor

Maybe you only try evaluating empty strings.

def makeNull(unitType):
  if unitType == "":
    return None
  else:
    return unitType‍‍‍‍‍
DanPatterson_Retired
MVP Emeritus

#---- snag the fat-fingered space placer ---
a = "    "
a.strip() == ""
Out[9]: True‍‍‍‍

0 Kudos
NickArcher
New Contributor

So the problem was I didn't have a else statement. I added in your else statement and it worked. Thanks.

0 Kudos