1

Disclaimer, I am terrible at writing my own functions especially in the field calculator. I have an address field (Address) with records that are missing street numbers. I would like to find these records, skip over them, and populate a field (Number) with the first element of the records that do have a street number. I would like to do this in the field calculator if possible (ArcMap 10.2.2, Basic).

Here's what I got so far:

def findNumbers(Address):
  if Address.split(" ")[0].isalpha():
    pass
  elif int(Address.split(" ")[0]) >= 0:
    Number = Address.split[0]

Number = 
def findNumbers(!Number!)
  • I see, but It's in the field calculator which requires a python syntax that is specific to calculations done in ArcMap. How would a non ArcMap python programmer be able to answer this? – geoJshaun Feb 13 '17 at 19:59
4

Regular expressions are nice for extraction.

import re
def extract(s):        
    num = re.search('^\d+', s)  # find all leading digits
    if num is None:
        return None
    else:
        return int(num.group())
  • 1
    import before def ? – FelixIP Feb 13 '17 at 20:54
  • @FelixIP, ideally yeah you would import in the python window. – Paul Feb 13 '17 at 20:57
  • @FelixIP, never realized the code block could have more than a def statement. It only runs that initially (e.g import time;time.sleep(5) adds only 5s to runtime not 5x#rows). Good to know! – Paul Feb 14 '17 at 22:25
  • @Paul I take it to extremes sometimes, gis.stackexchange.com/questions/193681/… – FelixIP Feb 14 '17 at 23:08
  • @FelixIP, that's awful, haha. +1 though for abusing field calculator. – Paul Feb 14 '17 at 23:28
1

This question is most certainly a duplicate, but anyway I'd use:

def getNumber(aString):
 aList=aString.split();n=len(aList)
 if n>1:
  try: aNumber= int(aList[0]);return aNumber
  except: return -1
 else: return -1
#-----------------------
getNumber(!address!)

providing there are now houses like 23B, i.e. non-numeric suffix is absent

  • Thank Felix. I did a search and didn't find anything, but I digress. – geoJshaun Feb 13 '17 at 20:05

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.