Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

if then statement in field calculator, string to integer

$
0
0
I am having a heck of a time trying to write an if elif script in the field calculator. I've read numerous threads but can't figure out how to do it right. I have a field called route_ty_1 that is a string field, with possible attributes of "Bus," "Rail," "LightRail," etc. For a given attribute, I want a code number to be returned for the site_code field, which is short integer. Here is the code I have been tinkering with:

Pre-logic script code:
Code:

def Reclass(site_code):
  if route_ty_1 == 'Bus':
    return 338
  elif route_ty_1 == 'LIGHT RAIL':
    return 281
  elif route_ty_1 == 'METRO':
    return 335
  elif route_ty_1 == 'Rail':
    return 282
  elif route_ty_1 == 'Commuter Bus':
    return 336

site_code =
Code:

Reclass(!site_code!)
If there is a better way to do this than if statements I am very open to suggestions! Thanks!

Viewing all articles
Browse latest Browse all 2485

Trending Articles