Select to view content in your preferred language

Right Trim String after Special Char

4809
7
02-25-2011 09:45 AM
John_S
by
Frequent Contributor
Ok, so I know it's probably something really simple that I'm missing, but, I need some help getting this to work.
I've got an attribute field !ADDRESS_GEO!.  This is a text field that has addresses in it, but they were user submitted addresses that include ";" and "#" type information.  An example address would be "123 W Main St ; STE 120" and "234 E Main St #D".  To geocode these using the geocode service I've got, I have to strip out the ";" and "#" characters as well as anything to the right of them and ideally leading and trailing spaces from the resulting string.  I'm using the Calculate Field geoprocessing tool in 9.3.1 Sp2.  I've tried a couple of expressions I thought would work to no avail, so, if anyone can help...

I've tried the expressions:
!ADDRESS_GEO!.rstrip(';')

and

!ADDRESS_GEO![:(!ADDRESS_GEO!.find(';'))]

If it makes any difference, the error I'm getting is:

"ERROR 000539: Error running expression: [:(.find(';'))] <type 'exceptions.SyntaxError'>: invalid syntax (<string>, line 1)"
Tags (2)
7 Replies
ChrisMathers
Deactivated User
try !ADDRESS_GEO!.split(';')[0]
0 Kudos
John_S
by
Frequent Contributor
I Tried and got the error:

"ERROR 000539: Error running expression: .split(';')[0]  <type 'exceptions.SyntaxError'>: invalid syntax (<string>, line 1)
Failed to execute (CalculateField)."
0 Kudos
ChrisMathers
Deactivated User
Uhh I hate trying things like this in the calculator. Get the python window out and just use an update cursor. Try this.

import arcpy
cursor=arcpy.UpdateCursor(yourFC)
for row in cursor:
    row.ADDRESS_GEO=row.ADDRESS_GEO.split(';')[0]
    cursor.updateRow(row)
del cursor
0 Kudos
John_S
by
Frequent Contributor
I'm still in Desktop 9.3.1, does arcpy actually work in 9.3.1?  I thought that was only at 10.
If only I could use a simple update cursor.
0 Kudos
John_S
by
Frequent Contributor
Thanks for all your suggestions and help.  I honestly have no earthly clue what was wrong with any of the above mentioned efforts to resolve the issue, but, I did manage to finally work out an "~ish" work-around that involved
export to Excel > text to columns tool > delete extra new columns > import Excel table into Database > create join based on ObjID (included in export to Excel) > calculate field in original table to match corrected field in joined table

I really would still love to figure out some effective calculate expression to resolve this quickly all within the database (since I will have to do this somewhat regularly for a new project I got), but, I guess I may have to wait on that until I am allowed by my 3rd party extensions to upgrade to V10 so I can use arcpy.

Thanks again for the suggestions.
0 Kudos
DanPatterson_Retired
MVP Emeritus
parser:  Python
Prelogic code section

def split_string(a_field):
  return a_field.split(";")[0]


expression section
split_string(!textfield!)  #where !textField! is the field you are trying to split
0 Kudos
KennethBrevard
New Contributor
Just to add on to this if anyone else stumbles across this thread trying to reverse concatenate a field.
I needed a simplified way to trim/split address numbers from a string in Python in the field calculator.   For example, for "199 MAIN ST"  I needed returned "MAIN ST".
Many ways to skin a cat but this can be done with an extension of the code provided above:


def split_string(a_field):
  return a_field.split(" ", 1)[1]


Codeblock:
split_string(!MY_FIELD!)
0 Kudos