I refer to Extracting numbers from string field using Python Parser of ArcMap Field Calculator?
The script above works fine for me, but I have to develop it a little. What I want is just to omit values without numbers. As you see in the table TEXTSTRING field with ID = 10 got only letters, it's Ok if Python leaves it empty, I just want make it go through all values in table.
target column is test_srednica2
Below you see my attempt :/
def makestr(TEXTSTRING):
lis = []
for i in TEXTSTRING:
if i.isdigit():
lis.append(i)
elif i.isalpha():
continue
return ''.join(lis)
Below i put code which has filled field "test_srednica2" As you see in table it is working until row "13" there is value "woc". There is no number so i think that`s why this function stops.
I want my function leave cells with letters only, and go throught all the others.
If you look on row "11" you see its going throught even if value is "<Null>" it
s OK for me.
def makestr(TEXTSTRING):
lis = []
for i in TEXTSTRING:
if i.isdigit():
lis.append(i)
return ''.join(lis)
-
What happened when you tried the provided answer?Bera– Bera2019年01月22日 19:46:18 +00:00Commented Jan 22, 2019 at 19:46
1 Answer 1
You want the code not to stop when input is Null or only letters?
Try:
def extractNumbers(sometextfield):
if (sometextfield is not None and any(char.isdigit() for char in sometextfield)):
return int(''.join([str(i) for i in sometextfield if i.isdigit()]))
else:
return None #Or 0 or whatever you want
Call with:
extractNumbers( !Textfield!)
Explore related questions
See similar questions with these tags.