ERROR 000539: SyntaxError from field calculator

746
3
10-31-2012 01:48 PM
ThomasStanley
New Contributor III
I want to parse a text field into a floating point field. (e.g.: "10-Inches Bituminous Concrete 8-Inches Portland Cement Concrete 6-Inches Crushed Stone Base" becomes "10")

Expression:
theBottom(!Description!)

CodeBlock:
def theBottom(desc):
  num = desc.find("-")
  if num < 0:
    return 0
  else:
    return float(desc[0:num])


Originally, I wanted to use multiple input fields in the expression, but I can't even get this bit to work. It gives this error: "ERROR 000539: SyntaxError: EOL while scanning string literal (<expression>, line 1)"

Any suggestions?
Tags (2)
0 Kudos
3 Replies
curtvprice
MVP Esteemed Contributor
I want to parse a text field into a floating point field. (e.g.: "10-Inches Bituminous Concrete 8-Inches Portland Cement Concrete 6-Inches Crushed Stone Base" becomes "10")


There are of course many ways to do this kind of thing. Here's one way:

>>> text = "10 asdf 12"
>>> [c for c in text if c.isdigit() or c.isspace()]
['1', '0', ' ', ' ', '1', '2']
>>> "".join([c for c in text if c.isdigit() or c.isspace()])
'10  12'
>>>


Here's a function you can use that does this:

def GetNum(text):
    text = text.strip()
    text = "".join([c for c in text if c.isdigit() or c.isspace()])
    try:
        num = float(text.split()[0])
    except:
        num = None
    return num


Here's an enhanced version that works if there are no spaces between the numbers:

def GetNum(text):
    text = text.strip()
    c1 = list()
    for c in text:
        if c.isdigit(): 
            c1 += c
        else: 
            c1 += " "
    try:
        num = float("".join(c1).split()[0])
    except:
        num = None
    return num
0 Kudos
ThomasStanley
New Contributor III
To clarify the question, I'm not really asking how to parse text, but how to get rid of the errors that prevent me from using the field calculator. This certainly isn't the first time that I've run into this sort of error. It looks like the fastest way to do this is to export and do the manipulations in Excel.
0 Kudos
curtvprice
MVP Esteemed Contributor
To clarify the question, I'm not really asking how to parse text, but how to get rid of the errors that prevent me from using the field calculator. This certainly isn't the first time that I've run into this sort of error. It looks like the fastest way to do this is to export and do the manipulations in Excel.


Unless you are working with more than 100,000 table rows. 🙂

If you are doing a detailed function like this in Calculate Field, I highly recommend throughly testing it on the python command line first -- much more efficient than running the Calculate Field tool and looking in the geoprocessing results for the error messages.

If you are using VBScript, most VBScript functions are similar to VBA (although the language under the hood is quite different), so you can test those using the VBA IDE to test in Excel or Word if you don't have the ArcMap VBA installed. (However, if it gets complicated I stick with Python now, I just use the default VBScript parser for simple calculations.)

You can paste entire functions to the ArcMap command line to test them. In fact, I did this with the above function.

>>> def GetNum(text):
    text = text.strip()
    c1 = list()
    for c in text:
        if c.isdigit(): 
            c1 += c
        else: 
            c1 += " "
    try:
        num = float("".join(c1).split()[0])
    except:
        num = None
    return num
<type return>
>>> GetNum("10-Inches Bituminous Concrete 8-Inches Portland Cement ")
10.0
0 Kudos