Field Calculator expression question!

1239
13
07-28-2011 08:17 AM
SeanKroencke
New Contributor
I have a field that has City, State, Zip in it. I'd like a new field with the zip code only in it, and then I'd like to be able to remove the zip from the original field. Is there a way to do this?

Possibly an expression that removes all #'s?
0 Kudos
13 Replies
DaleHoneycutt
Occasional Contributor III
First, read this blog post about concatenating fields using the Calculate Field tool.  It explains how to create a Python routine for Calculate Field to run to fetch a new field value.

Assuming you're dealing with 5-digit zip U.S. zip codes, and that the zip code falls at the end of the address, here's a snippet that shows one way you can parse it.

>>> address = "380 New York St Redlands Ca 92373  "  # Note: trailing blanks
>>> address = address.strip()  # get rid of leading/trailing blanks
>>> print address
380 New York St Redlands Ca 92373

>>> length_of_address = len(address)  # number of characters
>>> print length_of_address
33

>>> zip = address[length_of_address - 5: length_of_address]  # Get last 5 digits
>>> print zip
92373

>>> stripped_address = address[0:length_of_address - 5]  # everything up to zip code
>>> print stripped_address
380 New York St Redlands Ca 
0 Kudos
BruceNielsen
Occasional Contributor III
Another alternative would be to use zip = address.strip()[-1]. That will return the last set of non-whitespace characters in the address string, regardless of length. You could then use slicing with rfind to get the first portion of the string:

>>> address = "380 New York St Redlands Ca 92373  "
>>> zip = address.split()[-1]
>>> zip
'92373'
>>> address[:address.rfind(zip)]
'380 New York St Redlands Ca '
0 Kudos
SeanKroencke
New Contributor
Thanks for both of your replies! I failed to mention in my haste that some of the entries have not only the 5 digit US ZIP code but have the 4 digit ZIP tagged on at the end as well, for example:

"Town, NY 12345-1234"

Bruce, by last set of non-whitespace characters characters, what do you mean? I'm utterly inexperienced at coding/programming, pardon the questions.

Thanks for the help!
0 Kudos
BruceNielsen
Occasional Contributor III
Whitespaces are spaces, tabs, etc. When using the split method without any parameters, it also removes those extra characters from the end of the string. Technically, the split method creates a Python list of items from a string based on a delimiter character in the string. The default is a space. Since you only want the last item in that list (the zip code), the [-1] is added to tell Python to only return the last item in the list that split created.

If you want to learn more, start with the tutorial that is included with Python's help. IMHO it's a good place to get familiar with what Python can do as far as string manipulation.
0 Kudos
SeanKroencke
New Contributor
That would work perfectly! Do I need to replace where you have "address" with the name of the field that has the address in it, or am I NOT doing this calculation in a new blank field. I'm thinking that this is a calculation done within the same field.

Do I include the >>>'s?

I will certainly check out the tutorial for Python. Is that located within ArcGIS or separately?

>>> address = "380 New York St Redlands Ca 92373  "
>>> zip = address.split()[-1]
>>> zip
'92373'
>>> address[:address.rfind(zip)]
'380 New York St Redlands Ca '


Thanks
0 Kudos
BruceNielsen
Occasional Contributor III
I don't normally use Python within the Calculate Field tool, but I think you would use an expression like !fieldname!.split()[-1], where fieldname is the column with your existing city/state/zip string. The blog post that Dale referenced should provide enough details to get you started.

The '>>>' are prompts from Python's interactive interpreter. Don't include those in your code.

The Python Help is found under Python 2.x as Python Manuals if you're using ArcGIS 9.x. For ArcGIS 10, I think you go to ArcGIS, then Python 2.x, then look for the help docs.
DaleHoneycutt
Occasional Contributor III
Good one, Bruce. 

>>> address = "380 New York St Redlands Ca 92373  "
>>> zip = address.split()[-1]
>>> zip
'92373'
>>> address[:address.rfind(zip)]
'380 New York St Redlands Ca '
0 Kudos
SeanKroencke
New Contributor
Thanks again Bruce,
!fieldname!.split()[-1]
worked excellently in the field calculator!
0 Kudos
SeanKroencke
New Contributor
Any advice on how I can do the same technique to REMOVE the ZIP at the end, so I have a field that's just the City, State?
0 Kudos