0

enter image description here

This is a similar question to Extracting numbers from string field using Python Parser of ArcMap Field Calculator?

I have a similar issue in ArcMap 10.6. I have a field named 'SIZE' that holds number values and size unit (e.g. '200 mm' or '12"').

I am trying first to extract the numbers and deal with the units after. I created a new string field named 'SIZE2', while using the same code, but when I run it the values in 'SIZE2' stay NULL.

def makestr(SIZE):
 numlist = []
 for s in SIZE:
     if s.isdigit():
         numlist.append(s)
 return ''.join(numlist)

Any ideas?

0
2

The code is correct, currently you are pointing to the wrong field when using makestr(!SIZE2!) under SIZE2 = which is incorrect. You should point to the !SIZE! field instead of !SIZE2! field.

The correct expression is makestr(!SIZE!) when you executing the function under SIZE2 =.

enter image description here

Here is the result:

enter image description here

0

Here is an UpdateCursor approach which iterates through each row in the attributes, extracts the numeric values in the field SIZE and writes them to the field SIZE2.

import arcpy

fc = '/path/to/your.gdb/featureclass'

with arcpy.da.UpdateCursor(fc, ("SIZE", "SIZE2")) as cursor:
    for row in cursor:
        row[1] = ''.join([x for x in row[0] if x.isdigit()])
        cursor.updateRow(row) 

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.