Select to view content in your preferred language

Python field calculator syntax

1613
9
Jump to solution
01-31-2012 12:00 PM
MarcGuidry
Emerging Contributor
I keep getting a syntax error, but I'm not sure where the problem is.  Any suggestions?

The point of this is to concatenate a Last name field and First name field separated by a comma if there is both a last and first name.  If one of the fields is blank, it leaves out the comma (there are no nulls).

def name (!OWNER1_LAST_NAME!, !OWNER1_FIRST_NAME!) if len(!OWNER1_FIRST_NAME!)>0 and len(!OWNER1_LAST_NAME!)>0:   return !OWNER1_LAST_NAME! & ', ' & !OWNER1_FIRST_NAME! else:   return !OWNER1_FIRST_NAME!+!OWNER1_LAST_NAME!


name (!OWNER1_LAST_NAME!, !OWNER1_FIRST_NAME!)


Thanks,
Marc
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
Oh sorry, you need to take those exclamation points out.

def name1 (OWNER1_LAST_NAME, OWNER1_FIRST_NAME):   if len(OWNER1_FIRST_NAME)>0 and len(OWNER1_LAST_NAME)>0:     return OWNER1_LAST_NAME + ', ' + OWNER1_FIRST_NAME   else:     return OWNER1_FIRST_NAME+OWNER1_LAST_NAME


name1(!OWNER1_LAST_NAME!, !OWNER1_FIRST_NAME!)

Sorry it took so long to spot that, I don't use the calculator much, prefer UpdateCursor

View solution in original post

0 Kudos
9 Replies
DarrenWiens2
MVP Honored Contributor
Python's concatenate character is +, not &.
0 Kudos
MarcGuidry
Emerging Contributor
I corrected that, but there is still a syntax error.  see anything else?


def name (!OWNER1_LAST_NAME!, !OWNER1_FIRST_NAME!)
if len(!OWNER1_FIRST_NAME!)>0 and len(!OWNER1_LAST_NAME!)>0:
  return !OWNER1_LAST_NAME! + ', ' + !OWNER1_FIRST_NAME!
else:
  return !OWNER1_FIRST_NAME!+!OWNER1_LAST_NAME!


name (!OWNER1_LAST_NAME!, !OWNER1_FIRST_NAME!)
0 Kudos
MathewCoyle
Honored Contributor
Looks like missing colon at end of function definition

def name (!OWNER1_LAST_NAME!, !OWNER1_FIRST_NAME!): # add this
    if len(!OWNER1_FIRST_NAME!)>0 and len(!OWNER1_LAST_NAME!)>0:
        return !OWNER1_LAST_NAME! + ', ' + !OWNER1_FIRST_NAME!
    else:
        return !OWNER1_FIRST_NAME!+!OWNER1_LAST_NAME!
0 Kudos
MarcGuidry
Emerging Contributor
I've fixed that and there's still a syntax error.  Argh!

def name1 (!OWNER1_LAST_NAME!, !OWNER1_FIRST_NAME!):
  if len(!OWNER1_FIRST_NAME!)>0 and len(!OWNER1_LAST_NAME!)>0:
    return !OWNER1_LAST_NAME! + ', ' + !OWNER1_FIRST_NAME!
  else:
    return !OWNER1_FIRST_NAME!+!OWNER1_LAST_NAME!


name1 (!OWNER1_LAST_NAME!, !OWNER1_FIRST_NAME!)


Thanks!
0 Kudos
MathewCoyle
Honored Contributor
Did you change the indentation as my code shows as well?
0 Kudos
MarcGuidry
Emerging Contributor
I did.

This is the error I'm getting when I run it in the field calc gp tool.

ERROR 000989
Python syntax error: Parsing error : invalid syntax (line 1)

[ATTACH=CONFIG]11590[/ATTACH]
0 Kudos
DarrenWiens2
MVP Honored Contributor
Oh, right. You pass in the arguments as fields:

name1(!OWNER1_LAST_NAME!, !OWNER1_FIRST_NAME!)


In the code block, you reference the arguments as variables:

def name1 (lastname, firstname):
  if len(firstname)>0 and len(lastname)>0:
    return lastname + ', ' + firstname
  else:
    return firstname + lastname


See the example for Using Code Blocks here.
0 Kudos
MathewCoyle
Honored Contributor
Oh sorry, you need to take those exclamation points out.

def name1 (OWNER1_LAST_NAME, OWNER1_FIRST_NAME):   if len(OWNER1_FIRST_NAME)>0 and len(OWNER1_LAST_NAME)>0:     return OWNER1_LAST_NAME + ', ' + OWNER1_FIRST_NAME   else:     return OWNER1_FIRST_NAME+OWNER1_LAST_NAME


name1(!OWNER1_LAST_NAME!, !OWNER1_FIRST_NAME!)

Sorry it took so long to spot that, I don't use the calculator much, prefer UpdateCursor
0 Kudos
MarcGuidry
Emerging Contributor
Thanks folks.  I finally ended up using the UpdateCursor.  Worked like a charm.
0 Kudos