1

I have a feature class with label expression in VB script. I do not have the skill to convert a VB script to a python script.

Here is the VBScript:-

Function FindLabel ( [DESCRIPTION]  )
  if (left( [name] ,4) = "ABCD") then
   FindLabel =PCase( MID( [DESCRIPTION] , 19, 100))
  else
   FindLabel = PCase( [DESCRIPTION] )
  end if
End Function

Function PCase(strInput)
 Dim iPosition
 Dim iSpace
 Dim strOutput
 iPosition = 1
 Do While InStr(iPosition, strInput, " ", 1) <> 0
 iSpace = InStr(iPosition, strInput, " ", 1)
 strOutput = strOutput & UCase(Mid(strInput, iPosition, 1))
 strOutput = strOutput & LCase(Mid(strInput, iPosition + 1, iSpace - iPosition))
 iPosition = iSpace + 1
 Loop
 strOutput = strOutput & UCase(Mid(strInput, iPosition, 1))
 strOutput = strOutput & LCase(Mid(strInput, iPosition + 1))
 PCase = strOutput
 End Function

I came up with following python code. But it did not work:-

def FindLabel([DESCRIPTION]):
    if [name][:4]=='ABCD':
        FindLabel = PCase( [DESCRIPTION][19:100] )
    else:
        FindLabel = PCase( [DESCRIPTION] )

def PCase(strInput):
    iSpace=strInput.find(" ", beg=0, end=len(strInput))
    iPosition=1
    while iSpace!=0:
        iSpace=strInput.find(" ", beg=0, end=len(strInput))
        strOutput=strOutput+strInput[0:1].upper()
        strOutput=strOutput+strInput[iPosition+1:iSpace-iPosition].lower()
        iPosition=iSpace+1
    strOutput=strOutput+strInput[iPosition:1].upper()
    strOutput=strOutput+strInput[iPosition+1:].lower()
    return strOutput
  • Why do you want to convert it? – whatahitson Jan 26 '17 at 8:33
  • My ArcGIS for Server is installed in Linux. That's why. – Snehil Kandpal Jan 26 '17 at 8:43
  • 4
    Could you tell us what the script is trying to do and what errors you have seen? It looks like you're trying to convert the string to title case, so [DESCRIPTION][19:100].title() would do that. No need for PCase function. – jon_two Jan 26 '17 at 9:28
  • @SnehilKandpal Please edit your question to detail what your current VBS label code does, and what happens when you try to convert it to python. – Midavalo Feb 21 '17 at 23:36
  • There are many many issues with your code. The logic in VBScript does not translate directly into Python – Midavalo Feb 22 '17 at 0:50
3

You will get the result that I think you're after by using the code below.

This looks at the value of field name, and if it starts with ABCD it will return a label for field Description starting at the 19th character, and if the name field doesn't begin with ABCD it will return the entire Description value.

The label will capitalise the first letter of every word.

def FindLabel([Description], [name]):
    if [name][:4]=='ABCD':
        return [Description][19:100].title()
    else:
        return [Description].title()

The assumption is that records that have a name that starts with ABCD have a 19 digit code at the start of the Description that you want to strip off when labelling.

Note that [Description][19:100] will stop the label at the 100th character too. If you wish to include longer values you can use just [Description][19:]] which won't strip any text from the end of the label.


The code below is a rewrite of the code in the question, and will cope with punctuation in the Description string which str.title() may not allow for.

def FindLabel([Description], [name]):
    if [name][:4]=='ABCD':
        return PCase( [Description][19:100] )
    else:
        return PCase( [Description])

def PCase(strInput):
    strOutput = ""
    spc = strInput.find(" ", 0, len(strInput))
    while spc != -1:
        strOutput += strInput[:1].upper() + strInput[1:spc+1]
        strInput = strInput[spc+1:]
        spc = strInput.find(" ", 0, len(strInput))

    strOutput += strInput[:1].upper() + strInput[1:]

    return "'{}'".format(strOutput.strip())

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.