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)
  • What happened when you tried the provided answer? – BERA Jan 22 at 19:46
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.