Empty String on Field Calculator

5564
4
Jump to solution
10-25-2012 05:21 AM
ionarawilson1
Occasional Contributor III
I am taking a web course called "Use Python in ArcGIS Desktop 10" and one of the exercises is to write a python script on the field calcualtor to change the types of schools:

def label(name, type):
if type == "HIGH":
  return name + " HS"
if type == "MIDDLE":
  return name + " MS"
elif type == "ELEMENTARY":
  return name + " ELEM"

But the type column has one record with an empty string and I get an error because of that. I know I could use an "else" statement  to take care of that but I have had this issue before and would like to know how I can include that in the python script. I tried:

if  type == " ":
  return  name + " "


I also tried

if type is None:
return  name + " "

I also tried
if len(type) == 0:
return  name + " "

And a couple more statements, but nothing works. Do you guys know what I can use to account for the empty string? Thanks!!!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Occasional Contributor III
I'm using version 10.0 sp4 and the base code provided in exercise works. It just gives <Null> in MAP_LABEL field when TYPE field is empty.

To return only school name this code works for me (there's a space character in TYPE field which look like empty):
def label(name, type):   if type == "HIGH":     return name + " HS"   elif type == "MIDDLE":     return name + " MS"   elif type == "ELEMENTARY":     return name + " ELEM"   elif type == " ":     return name

This works too:
def label(name, type):   if type == "HIGH":     return name + " HS"   elif type == "MIDDLE":     return name + " MS"   elif type == "ELEMENTARY":     return name + " ELEM"   else:     return name

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus
the way you have written it is " "  which is a space not an empty string  ie.
>>> a = ""
>>> print len(a)
0
>>> b = " "
>>> print len(b)
1
>>>
0 Kudos
ionarawilson1
Occasional Contributor III
Hi Dan,

I also tried the way you mentioned but it does not work:

def label(name, type):
if  type == "" :
        return  name + " "
if type == "HIGH":
        return name + " HS"
if type == "MIDDLE":
      return name + " MS"
elif type == "ELEMENTARY":
     return name + " ELEM"

By the way, the return statement is indented, although it is not showing like it is here. Thanks
0 Kudos
MarcinGasior
Occasional Contributor III
I'm using version 10.0 sp4 and the base code provided in exercise works. It just gives <Null> in MAP_LABEL field when TYPE field is empty.

To return only school name this code works for me (there's a space character in TYPE field which look like empty):
def label(name, type):   if type == "HIGH":     return name + " HS"   elif type == "MIDDLE":     return name + " MS"   elif type == "ELEMENTARY":     return name + " ELEM"   elif type == " ":     return name

This works too:
def label(name, type):   if type == "HIGH":     return name + " HS"   elif type == "MIDDLE":     return name + " MS"   elif type == "ELEMENTARY":     return name + " ELEM"   else:     return name
ionarawilson1
Occasional Contributor III
Thank you m.gasior,

After applying the SP 4.0 the error went away!!! Thanks again!
0 Kudos