-2

I am using ArcGIS Desktop 10.2.1.

I am really new to Python and do not understand VB. I need to convert this VB script to Python.

The code is below. It believe it is looking for a numerical value within the field's attribute and then using that as the Interstate/Highway shield.

Function FindLabel ( [STREETNAME] )
' Extracts number from [STREETNAME]
' ---------------------------

  Dim sIn, sNew, i
  sIn = [STREETNAME]  ' input field value
  sNew = ""  'result
  i = 1

  ' check if char is numeric
  While i <= Len(sIn)    
    If IsNumeric(Mid(sIn, i, 1)) Then
      sNew = sNew & (Mid(sIn, i, 1))
    End If
    i = i + 1
  Wend
  FindLabel = sNew
End Function
2

Python lacks the isNumeric condition for the string. The best I could find is to use try/except on the int of the value (the crash 'n burn approach):

def FindLabel ( [STREETNAME] ):
    nVal = "" # set to an empty string just in case there's no numbers
    for part in [STREETNAME].split(): # breaks the string down to words and then steps over them as a variable called 'part'
        try:
            nVal = int(part) # convert this part of the string to an integer
            break # exit as soon as the first number is found
        except:
            pass # do nothing, we're expecting this failure
    return nVal

This function will return the first number in the string or an empty string if no numbers are found. It operates in essentially the same way as the VB script provided: iterates through the elements in the string until it finds a number and then returns that.

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.