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
30k11 gold badges53 silver badges108 bronze badges
asked Feb 13, 2017 at 19:49
1
  • 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? Commented Feb 13, 2017 at 19:59

2 Answers 2

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())
answered Feb 13, 2017 at 20:40
5
  • 1
    import before def ? Commented Feb 13, 2017 at 20:54
  • @FelixIP, ideally yeah you would import in the python window. Commented Feb 13, 2017 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! Commented Feb 14, 2017 at 22:25
  • @Paul I take it to extremes sometimes, gis.stackexchange.com/questions/193681/… Commented Feb 14, 2017 at 23:08
  • @FelixIP, that's awful, haha. +1 though for abusing field calculator. Commented Feb 14, 2017 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

answered Feb 13, 2017 at 20:03
1
  • Thank Felix. I did a search and didn't find anything, but I digress. Commented Feb 13, 2017 at 20:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.