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– geoJshaun2017年02月13日 19:59:37 +00:00Commented Feb 13, 2017 at 19:59
2 Answers 2
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
-
@FelixIP, ideally yeah you would import in the python window.Paul– Paul2017年02月13日 20:57:58 +00:00Commented 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!Paul– Paul2017年02月14日 22:25:12 +00:00Commented Feb 14, 2017 at 22:25 -
@Paul I take it to extremes sometimes, gis.stackexchange.com/questions/193681/…FelixIP– FelixIP2017年02月14日 23:08:51 +00:00Commented Feb 14, 2017 at 23:08
-
@FelixIP, that's awful, haha. +1 though for abusing field calculator.Paul– Paul2017年02月14日 23:28:16 +00:00Commented Feb 14, 2017 at 23:28
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– geoJshaun2017年02月13日 20:05:24 +00:00Commented Feb 13, 2017 at 20:05
Explore related questions
See similar questions with these tags.