1

I am unsure what syntax issue I am having with my If/Elif statement. I want to not use a column in my calculation if the value is blank. The end goal is to not have extra spaces before, after and between values.

Please be kind. I am still learning Python for scripting in ArcGIS Desktop.

Here is my Prelogic and Expression.

Prelogic:

def ifFields(SubAdd,LandM, ZipN):
 if SubAdd== " ":
 return "!LandmarkName! + ' ' + '('+ !ZipName! + ')'"
 elif LandM== " ":
 return "!SubAddress! + ' ' + '('+ !ZipName!+')'"
 elif ZipN== " ":
 return "!SubAddress! + ' ' + !LandmarkName!"

Expression:

ifField(!SubAddress!, !LandmarkName!, !ZipName!)
BBG_GIS
5,8555 gold badges47 silver badges88 bronze badges
asked Dec 7, 2017 at 20:06
6
  • 2
    What is your desired return? Could you give an example? I dont think it will return what you actually want Commented Dec 7, 2017 at 20:33
  • What my desired result are, the columns may have blanks and I do not want extra spaces after my concatenation. So if there are I want to populate the returned results as listed. Commented Dec 7, 2017 at 21:42
  • Don't forget to take the Tour which leads you through to advice on how to structure questions at gis.meta.stackexchange.com/a/3353/115 Your title should only summarize your question body, while your question body should contain all information pertinent to telling us what you want to do, what you have tried and where you are stuck. Commented Dec 7, 2017 at 23:06
  • It's not just best practice to set a value within the if block, then return it at the end, it's effectively required within inline helper functions. In addition, use the values passed into the function (the !var! is not valid in the code). Commented Dec 7, 2017 at 23:58
  • What results are you currently getting that are not desired? And, again, what are your desired results? Commented Dec 8, 2017 at 20:03

2 Answers 2

6

You define the function as ifFields but call it with ifField. They need to be the same.

Also I dont think it will return what you want.

Try changing:

"!LandmarkName! + ' ' + '('+ !ZipName! + ')'"

To:

'{0} ({1})'.format(LandM, ZipN)

And the same way for the elifs. If you dont want it to return None if none of the conditions are met, you need to end with an else.

answered Dec 7, 2017 at 20:29
2
  • Dang, I thought I fixed that ifFields issue. Good eye! Commented Dec 7, 2017 at 21:42
  • @RandyWolter if my answer answered your question could you accept it with the checkbox? Commented Dec 8, 2017 at 14:24
2

In addition to what was mentioned by BERA, you are also returning the field names as text, and referencing the field names, not the variable names. You code should look more like this:

def ifFields(SubAdd,LandM, ZipN):
 if SubAdd== " ":
 return LandM + ' ' + '('+ ZipN + ')'
 elif LandM== " ":
 return SubAdd + ' ' + '('+ ZipN+')'
 elif ZipN== " ":
 return SubAdd + ' ' + LandM

However this is not very nice and can cause issues when dealing with numbers. Better to use:

def ifFields(SubAdd,LandM, ZipN):
 if SubAdd== " ":
 return '{0} ({1})'.format(LandM, ZipN)
 elif LandM== " ":
 return '{0} ({1})'.format(SubAdd, ZipN)
 elif ZipN== " ":
 return '{0} {1}'.format(SubAdd, LandM)

That should do something and not error, but I am still unsure what your goal is. It might be better to use str.join on all the ones that exist, but again, I don't fully understand what your goal is here. If you have more than one thing blank this is going to get ugly. Also blanks can be None or '', so it might be good to check for those too.

answered Dec 7, 2017 at 20:51
6
  • Great tip. Will there be a space between the two field values? Also, I need the ZipN field to be in parenthesis. I'll test it out. Thanks. Commented Dec 7, 2017 at 21:44
  • Yes it will add the space. And parenthesis around the ZipN. Please read more about str.format here: docs.python.org/2/library/… Commented Dec 7, 2017 at 22:49
  • I want to add a forth line to the elif statement. I think it needs to be a else. Any suggestions to what I am doing wrong? 'def ifFields(SubAdd, LandM, ZipN): if SubAdd== "": return '{0} ({1})'.format(LandM, ZipN) elif LandM== "": return '{0} ({1})'.format(SubAdd, ZipN) elif ZipN== "": return '{0} {1}'.format(SubAdd, LandM) elif SubAdd=="" and LandM== "": return '(ZipN)' else: return '{0} {1} ({2})'.format(LandM, SubAdd, ZipN)' @isamson Commented Feb 9, 2018 at 21:47
  • Sorry. I can't get it to format correctly. Commented Feb 9, 2018 at 21:52
  • def ifFields(SubAdd, LandM, ZipN): if (SubAdd == "" and LandM == ""): return '({0})'.format(ZipN) elif (SubAdd <> "" and LandM <> ""): return '{0} {1} ({2})'.format(SubAdd, LandM, ZipN) elif (SubAdd == "" and LandM <> ""): return '{0} ({1})'.format(LandM, ZipN) elif (SubAdd <> "" and LandM == ""): return '{0} ({1})'.format(SubAdd, ZipN) Commented Feb 12, 2018 at 19:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.