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!)
Midavalo
  • 29,696
  • 10
  • 48
  • 104
geoJshaun
  • 1,535
  • 1
  • 14
  • 31
  • 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

2 Answers2

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())
Paul
  • 11,608
  • 1
  • 29
  • 47
  • 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, http://gis.stackexchange.com/questions/193681/calculating-sequential-numbers-into-sorted-table-using-arcgis-desktop/193684#193684 – 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

FelixIP
  • 22,922
  • 3
  • 29
  • 61