Extract values to the left of a certain character in a string

554
1
Jump to solution
05-09-2018 03:49 PM
Brian_McLeer
Occasional Contributor II

I received a table of data, and in that table is a field with latitude and longitude coordinates in one field. 

An example is: 45.4000/-122.700

I added two fields, latitude and longitude. 


Using field calculator I can successfully extract values after the '/' into the longitude field so that it is populated only with -122.700. 

The expression is:

!Latitude_Longitude![!Latitude_Longitude!.rindex('/')+1:]

I am not able to extract the values for latitude so that the latitude field will be populated just with 45.4000. I can also not perform this by using an exact number of characters, as the table of coordinates can have anywhere from 2 to 5 decimal places. Any help on how to extract all values to the left of the '/' would be appreciated. 

Brian
0 Kudos
1 Solution

Accepted Solutions
KimOllivier
Occasional Contributor III

The easiest way is to use a split() function with the split character. Switch to the python interpreter first. Because they are strings, convert them to floats as well.

!latitude! = float(!field!.split('/')[0]) for the left side

!longitude' = float(!field!.split('/')[1]) for the right side

View solution in original post

1 Reply
KimOllivier
Occasional Contributor III

The easiest way is to use a split() function with the split character. Switch to the python interpreter first. Because they are strings, convert them to floats as well.

!latitude! = float(!field!.split('/')[0]) for the left side

!longitude' = float(!field!.split('/')[1]) for the right side