How do you get Field Calculator to recognize spaces in a text field?

334
5
08-07-2017 04:29 AM
LukeHaskett
New Contributor

Hi!

I'm trying to run a field calculator statement to fill an integer field based on a preceding text field - so depending on what the text is, a certain number will be assigned to it. However, the field calculator statement I'm using only seems to work if the text has no spaces in it... any help with this would be greatly appreciated!

The expression is:

Reclass(!Typology!)

And the code black is:

def Reclass(Typology):
   if Typology is "Allotments":
      return 0.30
   elif Typology is "Amenity Green Space":
      return 0.40
   elif Typology is "Park and Recreation Ground":
      return 1.65

etc.

However, in the example above, only allotments will be assigned the requested number. If I removed the spaces from the other text, the calculation works but this isn't acceptable for what I need.

Many thanks in advance.

Luke

0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

change "is" to ==

if Typology == "Allotments":
LukeHaskett
New Contributor

Thanks Dan! I suspected it would be something so simple.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

glad it worked out Luke

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

NOTE: You Cannot input decimal numbers in Integer Fields. You need to create a field of Double/Float Datatype to input decimal numbers.

I used the geoprocessing tool Calculate Field—Help | ArcGIS for Desktop 

I used .strip() function to remove the leading and trailing spaces.

Expression:

Reclass(!Typology!)

Code Block:

def Reclass(parcel):
 parcel = parcel.strip()
 if (parcel == 'Allotments'):
  x=0.3
 elif (parcel == 'Amenity Green Space'):
  x=0.4
 elif (parcel == 'Park and Recreation Ground'):
  x=1.65
 return x‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍


Think Location
JoshuaBixby
MVP Esteemed Contributor

Not to get hung up on semantics, although I always do anyways, but you can input decimal numbers into integer fields using the field calculator.  That said, you should not because it converts the decimal to an integer and some of the information is lost.  But then again, maybe converting/casting the type is fine or desirable in some circumstances.  Oddly, I always thought the field calculator would just truncate a decimal, but a quick test shows it rounds.