3

The labels for my feature SSA_NAME are approximately 5 words long. I would like to shorten the labels to only 2 words. Every time I try a different code I receive either an error message or my layer is not labelled at all. Here is a code I tried based on another person's StackExchange question.

def convertLabel([SSA_NAME]): S= [SSA_NAME] S = S.split(" ")1 return S

enter image description here

  • Hi @Aaron , I just tried replacing the [] with !! but I'm still receiving the same error. def convertLabel(!SSA_NAME!): S= !SSA_NAME! S = S.split(" ")[1] return S – WolverineTime Jul 1 '15 at 20:42
  • No, you just need !! in FieldCalculator, not in the label expression. – mr.adam Jul 1 '15 at 20:55
2

This works for me and I have labeling only with 2nd word:

enter image description here

So maybe you have few atributres without " ", and you get "out of index".

def label([SSA_NAME]):
    x = [SSA_NAME]
    x1 = x.split(" ")
    if len(x1)>1:
        return x1[0] + " " + x1[1]
    else:
        return x1[0]
  • You should be able to shorten this to one line, no need for the FindLabel function: [SSA_NAME].split(" ")[1] If you want the first two words, you could just use [SSA_NAME].split(" ")[0] + " " + [SSA_NAME].split(" ")[1] Or, slice the list with [:1], but that may create a funny looking string – mr.adam Jul 1 '15 at 20:58
  • @mr.adam, I just tried [SSA_NAME].split(" ")[1] but in 'basic' the error was "No Features found. Could not verify expression." 'Advanced' gave me a different error. – WolverineTime Jul 1 '15 at 21:09
  • @dmh126, the code in your pasted pic worked for getting the second word but it did not allow the first and second to appear together. I tried [:1] instead of [1] but that just made the label disappear. – WolverineTime Jul 1 '15 at 21:11
  • 2
    return x1[0] + " " + x1[1] should works – dmh126 Jul 1 '15 at 21:13
  • @dmh126, that did the trick. I'm new to coding so thank you! – WolverineTime Jul 1 '15 at 21:20

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.