2

I am working on a task that will calculate a field to fix errors stemming from the Standardize Addresses tool and human error with the input data.

The script is below. I'm getting Error 000989 "Python syntax error: Parsing error SyntaxError: invalid syntax (Line 6)"

Note: ADDR_SN and ADDR_SD seems to be a truncated output of Street Name and Street Direction (There is some confusion as the Standardize Address tool is new to me)

Any pointers to help fix this code?

Expression:

AddSN2( !ADDR_SN!, !ADDR_SD!)

ADDR_SN is the Street Name and ADDR_SD is the street direction.

Code Block:

def AddSN2(StNa, STDir):
    if (StNa == "BEND"):
        return (STDir+ +StNa)
    elif (StNa == "CEDAR GARDEN"):
        return (StNa.replace(" ", "")
    else:
        return StNa

Script Table

8
  • 2
    I dunno if this is your issue, but here: return (STDir+ +StNa) should be return (STDir+ ' ' +StNa)
    – Jvhowube
    Commented Feb 6, 2017 at 18:24
  • This is a helpful tip, but it didn't clear the syntax error.
    – Jbarne
    Commented Feb 6, 2017 at 18:27
  • Is the field length of AddSN2 long enough?
    – Bera
    Commented Feb 6, 2017 at 18:27
  • 1
    The row indentation is wrong for the first return statement
    – Bera
    Commented Feb 6, 2017 at 18:29
  • 1
    @BERA although it might look/seem wrong, python will still accept it as there are no other lines in that block with a different indentation
    – Midavalo
    Commented Feb 6, 2017 at 18:47

2 Answers 2

7

You have a lot of unnecessary and one unclosed parentheses. The unclosed one in line 5 is causing the syntax error to be thrown in line 6. To correct and clean up, try this:

def AddSN2(StNa, STDir):
    if StNa == "BEND":
        return STDir+" "+StNa
    elif StNa == "CEDAR GARDEN":
        return StNa.replace(" ", "")
    else:
        return StNa
1
  • +1 @Jbarne also note that this fixes the + + in line 3 as well (which would have raised an error once the missing parentheses is added into line 5)
    – Midavalo
    Commented Feb 6, 2017 at 18:35
3

The existing answer certainly addresses your syntax errors. But just from a "code review" perspective that I always found useful when I was learning how to program, here's a more advanced way to write this kind of code that is a bit more generic and therefore easier to extend:

replacements = {
  "BEND": lambda name, dir: " ".join([name, dir)],
  "CEDAR GARDEN": lambda name, dir: name.replace(" ", "")
}

default = lambda name, dir: name

def format_addresses(name, dir):
    if name in replacements:
        formatting_function = replacements[name]
    else:
        formatting_function = default
    return formatting_function(name, dir)

Then, as you go on, it's a bit easier to extend replacements with more formatters: just add a new key/value, with each value being a lambda function that takes the name and direction values). Actually, they don't need to be lambda functions, if you need a bit more logic in a formatter then they can be references to a named function. The point is that it means you only need to adjust the replacements variable, and not the format_addresses function itself.

Another way to write that format_addresses function is:

def format_addresses(name, dir):
    return replacements.get(name, default)(name, dir)

This is even more advanced again, and probably a bit too confusing to be worth it. However I've included it because if you do get your head around it, you'll understand a bit more Python, which can only help you in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.