Select to view content in your preferred language

Splitting text strings

1453
5
Jump to solution
07-02-2012 05:39 AM
by Anonymous User
Not applicable
I am working on an addressing project for my county and I am trying to split an address field from parcel data that we have.  I have been using the !Field!.split(" ")[0] function to split them by spaces.  The problem is I want to get street names that have 2 and 3 words to be separate from the N, S, E, and W prefixes and not all of the records in the field have the N or S before the street name so doing it by order of the spaces is not really going to work.  I am wondering if there is any sort of .length() type function to select only one letter parts of a record in a string (the N, S, E, or W) and put it into a field by itself so I know which addresses I need to populate the prefix for?  Or what is the best way to split fields when I have street names like Valley Forge Dr, N Main St, and King Ave?  I need the direction in one field, the street name in another (one and two word street names), and the suffix in the last field. I know to get just the suffix I can use the !Field!.split(" ")[-1], but is it possible to just get the prefix from the records that have one?  If anyone can help please let me know! Thanks!

Caleb
GIS Technician
Cedar County, IA
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Here are a couple functions you can use to get the prefix, street name, and suffix.  You would need to make some minor changes to get this to work in the field calculator.

def prefix():     rows = arcpy.UpdateCursor(table)     for row in rows:         if len(row.FullAddr.split(" ")[0]) == 1:             row.Prefix = row.FullAddr[0]             rows.updateRow(row)     del row, rows  def suffix():     rows = arcpy.UpdateCursor(table)     for row in rows:         row.Suffix = row.FullAddr.split(" ")[-1]         rows.updateRow(row)     del row, rows  def street():     rows = arcpy.UpdateCursor(table)     for row in rows:         if len(row.FullAddr.split(" ")[0]) == 1:             row.Street = ' '.join(row.FullAddr.split(" ")[1:-1])             rows.updateRow(row)         else:             row.Street = ' '.join(row.FullAddr.split(" ")[0:-1])             rows.updateRow(row)     del rows, row  prefix() suffix() street()

View solution in original post

0 Kudos
5 Replies
markdenil
Frequent Contributor
There is indeed a length test:   len()

if len(!Field!.split(" ")[0]) == 1:
    you have a one letter prefix
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Here are a couple functions you can use to get the prefix, street name, and suffix.  You would need to make some minor changes to get this to work in the field calculator.

def prefix():     rows = arcpy.UpdateCursor(table)     for row in rows:         if len(row.FullAddr.split(" ")[0]) == 1:             row.Prefix = row.FullAddr[0]             rows.updateRow(row)     del row, rows  def suffix():     rows = arcpy.UpdateCursor(table)     for row in rows:         row.Suffix = row.FullAddr.split(" ")[-1]         rows.updateRow(row)     del row, rows  def street():     rows = arcpy.UpdateCursor(table)     for row in rows:         if len(row.FullAddr.split(" ")[0]) == 1:             row.Street = ' '.join(row.FullAddr.split(" ")[1:-1])             rows.updateRow(row)         else:             row.Street = ' '.join(row.FullAddr.split(" ")[0:-1])             rows.updateRow(row)     del rows, row  prefix() suffix() street()
0 Kudos
by Anonymous User
Not applicable
Thank you for the quick response...However, I am still having problems with it.  This could be because I am pretty new to python but I threw this into the field calculator and it gave me the errors with geoprocessing message:

if len(!ADDRESS!.split(" ")[0]) == 1

This did not work for some reason.  Any other suggestions? For beginners? 🙂

Caleb
0 Kudos
by Anonymous User
Not applicable
And thank you Jake, I just now saw your reply.  I will try this and see if I can get it to work.  I am no expert with python so I will see how much of this code I can understand and try to modify it to work. 

Caleb
0 Kudos
by Anonymous User
Not applicable
Jake,

Thank you very much for that update cursor!  It worked like a charm!

Caleb
0 Kudos