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
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 18, 2014 at 18:24
5
  • What errors or issues did you have? Commented Aug 18, 2014 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)." Commented Aug 18, 2014 at 18:36
  • What are the field types for a1, a2, a3, and are there Null values for any? Commented Aug 18, 2014 at 18:45
  • All three field types are String and there are some null values. Commented Aug 18, 2014 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. Commented Aug 18, 2014 at 21:37

2 Answers 2

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.

answered Aug 18, 2014 at 21:55
4
  • 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! Commented Aug 18, 2014 at 22:12
  • @MichaelMiles-Stimson OK, I see how it works. Commented Aug 18, 2014 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. Commented Aug 18, 2014 at 22:51
  • +1 for a nice pythonic approach that isn't limited to only 3 fields with set names. Commented Aug 19, 2014 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:
answered Aug 18, 2014 at 18:45
4
  • 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 ` Commented Aug 18, 2014 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 Commented Aug 18, 2014 at 19:34
  • your first elif statement is checking a2 twice, I'm assuming you meant to check a2 and a3. Commented Aug 18, 2014 at 19:46
  • @papadoo you are right, it's a typo. Corrected by now. Commented Aug 19, 2014 at 5: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.