I need to calculate the PIPEID field to equal the G_KEY_COMP field if PIPEID is null or PIPEID and G_KEY_COMP do not match. I think the first condition of the PIPEID being null is covered by the second condition of the fields not matching? I'm a novice on this topic and can't get the syntax I have to work. Here's what I've piecemealed together:

Pre-Logic Script Code:

def update(PIPEID,G_KEY_COMP):
  return G_KEY_COMP
 return PIPEID


I'm posting a screenshot of the field calculator window since I'm not able to get the indentation right in the codeblock.

enter image description here

  • if and else need to be indented the same amount, inside the function – Barbarossa Mar 28 '14 at 17:56
  • also there is a missing : at the end of the if – radouxju Mar 28 '14 at 19:50

Your code needs to be:

def update(PIPEID,G_KEY_COMP):
  if PIPEID is None:
    # PIPEID is NULL
    return G_KEY_COMP
    # PIPEID has a different value to G_KEY_COMP
    return G_KEY_COMP
    return PIPEID
  • Thanks, I tried this snippet and it gives a syntax error 000989 on line 5. Not sure why as it looks good to me. Also, thank you for including comments :) – Steve Mar 28 '14 at 19:49
  • 1
    again, : is missing after the second if – radouxju Mar 28 '14 at 19:51

Pythonic inline if is working in Field Calculator expression

res=(on_false, on_true)[condition]

arcpy.CalculateField_management("fc" , "field" ,"(!PIPEID!, !G_KEY_COMP!)[!PIPEID! != !G_KEY_COMP!]" , "PYTHON_9.3")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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