[GIS] Field calculator to copy numeric values from other fields

arcgis-10.2arcgis-desktopfield-calculatorpython

FieldA contains text: both numeric(123) and alpha-numeric(123a, 2-3, 3/2, '). FieldB is longstring. I want to use field calculator and codeblock on FieldB to bring from Field A only the numeric values.

while True:

try:
    val = int(x)
    print (val)
    break
except ValueError:
    print ("non-numeric")
    break

I tried something similar to the code seen here, but the process fails at some point. I just need something to see if fieldA .isdigit and if true copy to fieldB(if false pass a string "non-numeric"). Or anything similar.

I am using ArcGIS 10.2 for Desktop.

Best Answer

Have a look at this bit of code as your fieldB is of type string it does not need to do any type conversion.

This bit of python goes in the code block:

def numonly(s):
  if s.isdigit():
    return s
  else:
    return ""