Extract string in field calculator using python

5295
8
12-02-2011 07:15 AM
StephanieSnider
Occasional Contributor III
I'd like to use python in the field calculator to extract a string.  Basically the UTM North and East coordinates are in the same field and I'd like to parse them out.  An example of a field value is "North: 4479928.51 East: 290941.79".  When I run this very simple function in python, it works fine:
>>> def utmn(inValue):
utmn_start = inValue.find(":") + 2
utmn_stop = inValue.find("East") -1
return inValue[utmn_start:utmn_stop]
>>> utmn(field)
'4479928.51'

However, when I try to run it in the ArcGIS field calculator, i get an error message that the row contains a bad value.  None of the rows are null or contain text that is not in this format.  Here's what I put in the field calculator.  MERIDIAN_NAME_TEXT is the name of the field from which I am extracting the string.  UTM_North is the name of the input field.

Pre-Logic Script Code:
def utmn(inValue):
  utmn_start = inValue.find(":") + 2
  utmn_stop = inValue.find("East") -1
  return inValue[utmn_start:utmn_stop]

UTM_North =
utmn(!MERIDIAN_NAME_TEXT!)
Tags (2)
0 Kudos
8 Replies
AnthonyTimpson2
Occasional Contributor
I would honestly just calculate with VB 

for the Easting Field i would use

Right([Field], 9)  {[Field] is the name of the field, 9 is the number of spaces you want to include for the easting coordinate}


and for the Northing Field I'd do a find and replace for North: and for East:  Replacing it with nothing and leaving the Coordinate
0 Kudos
StephanieSnider
Occasional Contributor III
The coordinate value length is inconsistent.  That's why I do the find to determine the stop and start index for extraction.  I'd prefer to do this in python as I plan to do future field calculations using python.  But thanks for your suggestion.
0 Kudos
curtvprice
MVP Esteemed Contributor
I'd use a slightly different, more "pythonesque" approach using a list:
>>> str = "North: 4479928.51 East: 290941.79"
>>> spam = str.split()
>>> spam
['North:', '4479928.51', 'East:', '290941.79']
>>> x,y = float(spam[1]),float(spam[3])
>>> x
4479928.5099999998
>>> y
290941.78999999998
>>>
0 Kudos
StephanieSnider
Occasional Contributor III
That works Curtis, thanks.  However, I'd still like to know why my definition worked in python IDLE but not the field calculator.  I'm trying to improve my skills and just don't see why my definition would have failed.
0 Kudos
curtvprice
MVP Esteemed Contributor
(I) don't see why my definition would have failed.


Did you try this? Since your input field is text value you may need to put it in quotes. I believe the actual value gets substituted in before the Python expression is interpreted.

UTM_North =
utmn("!MERIDIAN_NAME_TEXT!") 
0 Kudos
StephanieSnider
Occasional Contributor III
No, that didn't work either.  Error 539 - error running expression.  I've had this issue before where a function will work in python and not ArcGIS.  I keep thinking there is some small thing I'm not doing, that ArcGIS requires and python doesn't.  Oh well.  Thanks for your help!
0 Kudos
ScottOatley
New Contributor II
Hello,

I ran your code and it worked. Check the field size and type of UTM_North. If the field is too small you'll get the error you mention. I assume you'd also get an error if you were trying to write into a FLOAT field since your function is returning a string. If that's the case then try converting with:
float(utmn(!MERIDIAN_NAME_TEXT!))
OR
return float(inValue[utmn_start:utmn_stop])


Also note, I think your stop value with -1 makes the last character the space before "East" so maybe use -2.

And I agree with Curtis regarding his suggested code for this particular task.

Scott
0 Kudos
StephanieSnider
Occasional Contributor III
That all makes sense.  I had the field size about 4 characters larger than I thought it needed to be, but perhaps there is a value in there that is bigger.  Thanks for your advice 🙂  I'll give it a try.
0 Kudos