3

The fieldname only has three characters. I dont want to display zeros in the first and third values. example below.

010 = 1

100 = 10

01Y = 1Y

10Y = 10Y

I have figured out how to strip out zeros except for the second value.

7

Use Python as the parser, and check the Advanced box. Then just replace yourFieldName with the name of the field you want to label.

def FindLabel([yourFieldName]):

    value = [yourFieldName]

    first = False
    third = False

    if value[0] == "0":
        first = True
    if value[2] == "0":
        third = True

    if first == True and third == False:
        return value[1:]

    elif first == False and third == True:
        return value[0:2]

    elif first == True and third == True:
        return value[1:2]

    else:
        return value
0
5

For brevity's sake:

def FindLabel ([field]):
  val = [field]
  return ''.join([j for i, j in enumerate(val) if j != '0' or i == 1])

Step through the string and toss away all 0's unless it's in the middle position (index of 1), then join what's left.

1
  • Thank you. This worked great. I am still learning. Going to look into how that third line works.
    – ajd018
    May 21 '15 at 12:57
3

Just for good measure, here's a way to use python string methods. This would work with strings of variable length, though I know that wasn't a consideration in this case:

def FindLabel(value):

    if value.endswith("0"):
        value = value[:-1]  # slice all but the last character if it's a 0
    return value.lstrip("0") # take off any leading zeros
3

Here's a little more concise way of doing it. Basically convert it to a list and replace list[0] and list[2] with '' if the value is 0

def FindLabel([field]):
    value = [field]
    l_value = [x for x in value]    # turns string to list

    no_zero_positions = [0,2]       # where you don't want zeros
    for p in no_zero_positions:
        if l_value[p] == '0':
            l_value[p] = ''         # Replace zero with empty string

    return ''.join(l_value)        # list back to string
3
  • So replace value with the fieldname? I am getting errors running this exactly as it is written.
    – ajd018
    May 20 '15 at 18:48
  • change it to for i in no_zero_positions
    – mr.adam
    May 20 '15 at 18:54
  • yes, I noticed that and changed it to a p instead of an i. And yes just replace !YOUR_FIELD! with your field name
    – JasonT
    May 20 '15 at 19:09

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.