1

I am trying to combine three numeric fields together for a feature's label. I only want to label the feature if it is over 0. The labeled numbers need to be separated a dash ("-"). This is what I attempted to use as the label expression in ArcMap 10.2:

def FindLabel ( [a1], [a2], [a3] ):
    if ([a3]) == 0 and int([a2]) == 0 and ([a1]) == 0:
        label = " "
    elif ([a3]) == 0 and int([a2]) == 0
        label = [a1]
    elif ([a3]) == 0
        label = [a1] + "-" + [a2]
    else:
        label = [a1] + "-" + [a2] + "-" + [a3]
    return label
  • What errors or issues did you have? – papadoo Aug 18 '14 at 18:30
  • I get this when I try to verify the expression or hit OK: "Error 0 on line 0. SyntaxError: invalid syntax(<string>, line 4)." – juturna Aug 18 '14 at 18:36
  • What are the field types for a1, a2, a3, and are there Null values for any? – recurvata Aug 18 '14 at 18:45
  • All three field types are String and there are some null values. – juturna Aug 18 '14 at 18:53
  • You're missing the colon on the end of the elif expressions, that would be why it's not working. For example change elif ([a3]) == 0 and int([a2]) == 0 to elif ([a3]) == 0 and int([a2]) == 0: for correct syntax. If you have null values the expression is [A3] == None for null. – Michael Stimson Aug 18 '14 at 21:37
4

This function doesn't need to be too complex. Convert the items you want into a list, then join the result.

def FindLabel([A1], [A2], [A3]):
    args = [A1, A2, A3]
    items = [str(x) for x in args if bool(x) and int(x) > 0]
    if any(items):
        return '-'.join(items)
    else:
        return ' '

If this were proper Python, you would normally use *args to accept any number of arguments into a list.

  • That's not how an ArcGis label expression works MikeT, have a read of this resources.arcgis.com/en/help/main/10.1/index.html#//… that functionallity will work if you supply that as another sub and call it from the find_label sub - that name is special, but if you name it join_label and call it from find_label it works great! – Michael Stimson Aug 18 '14 at 22:12
  • @MichaelMiles-Stimson OK, I see how it works. – Mike T Aug 18 '14 at 22:45
  • Awesome! Great expression for its simplicity and diversity: '_'.join, ' '.join etc.. works just as well. I like the use of any(items), that's not one I've seen before, usually I do if len(items) != 0 but that can mean a list of empty items. Thanks for that, I'll be using it in the future. – Michael Stimson Aug 18 '14 at 22:51
  • +1 for a nice pythonic approach that isn't limited to only 3 fields with set names. – Paul Aug 19 '14 at 0:41
3

There are missing semi-colons and problems with the parentheses. then you should make sure that you always have a string output.

EDIT : sorry, I've given a solution for the field calculator. This solution is updted for the labelling engin.

def FindLabel ( [A1], [A2], [A3] ):
    a1=[A1]
    a2=[A2]
    a3=[A3]
    if ( (int(a1) == 0) and (int(a2) == 0) and (int(a3) == 0)):
        label = " "
    elif ( (int(a2) == 0) and (int(a3) == 0):
        label = str(a1)
    elif (int(a3) == 0):
        label = a1 + "-" + a2
    else:
        label = a1 + "-" + a2 + "-" + a3
    return label

note that the following test are equivalent

 int(a1) == 0 

and

 a1='0'

for testing if a field is null, you can use

 if not a1:
  • Thanks for the response. I'm (very) new to Python. My fields are all Strings. I tried the code you wrote, as well as the following (since my fields are Strings). Neither worked and I get the same error. ` def FindLabel ( a1, a2, a3 ): if ( (int(a3) == 0) and (int(a2) == 0) and (int(a1) == 0)): label = " " elif ( (int(a3) == 0) and (int(a2) == 0): label = str(a1) elif (int(a3) == 0): label = str(a1) + "-" + a2 else: label = str(a1) + "-" + a2 + "-" + str(a3) return label ` – juturna Aug 18 '14 at 18:57
  • +1 for the edited solution for labelling. If this doesn't work I'd guess that something is wrong with the data – GISKid Aug 18 '14 at 19:34
  • your first elif statement is checking a2 twice, I'm assuming you meant to check a2 and a3. – papadoo Aug 18 '14 at 19:46
  • @papadoo you are right, it's a typo. Corrected by now. – radouxju Aug 19 '14 at 5:19

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.