4

I would like to find out how to properly format a conditional expression in labeling, so as to only get specific characters back out of a field that has 9 characters in it, but don't wish to label them on the map in full.

Sadly, I am not the best in scripting languages, I know a bit SQL but I see labeling or does VBscript, Jscript, or Python.

I've thus far been able to drop the first two characters, but I am not familiar how to get to the part where I keep certain characters and skip others.

Example data--- [FACILITYID] WF2820001, WF3540034, etc...

What I have so far built in VBscript is as follows

Function FindLabel ( [FACILITYID] )
  FindLabel = mid ([FACILITYID],3,50)
End Function

This is at least dropping off the WF that doesn't need labeled. I wish to then have two characters remain, insert a '-' symbol, keep one character, drop the zeros and replace with a tilda ~ symbol and keep the last one or two numbers if their anything higher than 01.

Example data would have this input [FACILITYID] WF2820001, WF3540034 and would then come back with 28-2~1, 35-4~34.

5

Switch to Python parser, click Advanced and enter the following code

def FindLabel ( [FACILITYID] ):
    exValue = [FACILITYID]
    first = exValue[2:4]
    second = exValue[4:5]
    third = int(exValue[5:])

    newLabel = "{0}-{1}~{2}".format(first, second, third)
    return newLabel

This removes the first two characters, keeps the second two, adds a -, keeps the next one, adds a ~, keeps the remaining digits (stripping off the leading zeros by converting to integer)

The [2:4] etc. is known as "Slicing" - in this case it takes the third character in the string (remember that in Python you count from 0) and stops at the fifth character (excluded).

Python character positions:

exValue = 'WF3540034'
           012345678

so the `[2:4] gives the 3 and 5, but nothing from the 4 onwards.

exValue [2:4] returns two digits, in positions 2 and 3 -> 35
exValue [4:5] returns a single digit, in position 4 -> 4
exValue [5:] returns everything from position 5 onwards -> 0034
int(exValue [5:]) converts the 0034 to an integer (number) so no leading zeros -> 34

For more information, have a look at An Informal Introduction to Python - Strings

  • This is fantastic! It works like a charm. I'll be honest I only understand/can see how part of it works. I really need to learn Python. – twweepie Jun 13 '16 at 18:45
  • I'm walking at the moment, doing this on my phone - I'll update with more explanation soon – Midavalo Jun 13 '16 at 18:49
  • 1
    @twweepie I've added a bit of an explanation which hopefully makes it a bit more understandable – Midavalo Jun 13 '16 at 19:56
  • Thank you very much for that write up and explanation. It looks complicated at first but when you break it down, it starts making a lot of sense on what its doing. – twweepie Jun 14 '16 at 11:54
3

Pretty elegant answer here led me to work up this python sample code which you can tweak to your advantage. It converts the field value to a list and then you can modify the list values as needed.

MH54-201

def FindLabel ( [MH_No] ):
 l = list([MH_No])
 l[4] = '~'
 return "".join(l)

Returns

MH54~201

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.