0

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)

enter image description here


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>" its OK for me.

def makestr(TEXTSTRING):
 lis = []
 for i in TEXTSTRING:
 if i.isdigit():
 lis.append(i)
 return ''.join(lis)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 22, 2019 at 12:45
1
  • What happened when you tried the provided answer? Commented Jan 22, 2019 at 19:46

1 Answer 1

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!)

enter image description here

answered Jan 22, 2019 at 13:31
0

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.