Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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):
 if PIPEID != G_KEY_COMP
  return G_KEY_COMP
else:
 return PIPEID

PIPEID = update(!PIPEID!, !G_KEY_COMP!)

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

share|improve this question
    
if and else need to be indented the same amount, inside the function –  Barbarossa Mar 28 at 17:56
    
also there is a missing : at the end of the if –  radouxju Mar 28 at 19:50

2 Answers 2

up vote 3 down vote accepted

Your code needs to be:

def update(PIPEID,G_KEY_COMP):
  if PIPEID is None:
    # PIPEID is NULL
    return G_KEY_COMP
  if PIPEID != G_KEY_COMP:
    # PIPEID has a different value to G_KEY_COMP
    return G_KEY_COMP
  else:
    return PIPEID
share|improve this answer
    
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 at 19:49
1  
again, : is missing after the second if –  radouxju Mar 28 at 19:51
    
Awesome! Thanks for the help guys! –  Steve Mar 28 at 19:55

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")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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