AnsweredAssumed Answered

Extract the largest Number from a String

Question asked by NatiNati1186 on Jun 18, 2020
Latest reply on Jun 18, 2020 by AthensGIS

Hello,

 

I am trying to extract the largest number from a string. 

More details:

I have a feature class with a text field. Each feature has a string like this: "3, 5, 3.9, 2.5 FAR Values" and I need to extract the higher number and put it in a new field. From that string I would need number 5.

There are some features that have Null values and some with just text and no numbers.

I wrote the following script using a python function from the internet but I am not sure how to apply it in Arcpy.

import arcpy

# find largest number in a string

arcpy.env.workspace = r"D:\APRX_MXDS\USA_App_Project\usa_parcels_with_FARField.gdb"
arcpy.env.overwriteOutput = True

fc = "temp"

with arcpy.da.UpdateCursor(fc, "FAR_INTEGER") as cursor: # Loop through each feature
    for row in cursor:
        ls = list()
        for w in row[0].split():
            try:
                ls.append(int(w))
            except:
                pass
            try:
                return max(ls)
            except:
                return None

Outcomes