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!)
2 Answers 2
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.
-
Dang, I thought I fixed that ifFields issue. Good eye!Randy Wolter– Randy Wolter2017年12月07日 21:42:45 +00:00Commented Dec 7, 2017 at 21:42
-
@RandyWolter if my answer answered your question could you accept it with the checkbox?Bera– Bera2017年12月08日 14:24:47 +00:00Commented Dec 8, 2017 at 14:24
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.
-
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.Randy Wolter– Randy Wolter2017年12月07日 21:44:38 +00:00Commented 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/…isamson– isamson2017年12月07日 22:49:43 +00:00Commented 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)' @isamsonRandy Wolter– Randy Wolter2018年02月09日 21:47:10 +00:00Commented Feb 9, 2018 at 21:47
-
Sorry. I can't get it to format correctly.Randy Wolter– Randy Wolter2018年02月09日 21:52:58 +00:00Commented 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)
Randy Wolter– Randy Wolter2018年02月12日 19:19:59 +00:00Commented Feb 12, 2018 at 19:19
Explore related questions
See similar questions with these tags.
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).