theBottom(!Description!)
def theBottom(desc): num = desc.find("-") if num < 0: return 0 else: return float(desc[0:num])
To clarify the question, I'm not really asking how to parse text, but how to get rid of the errors that prevent me from using the field calculator. This certainly isn't the first time that I've run into this sort of error. It looks like the fastest way to do this is to export and do the manipulations in Excel.
>>> def GetNum(text): text = text.strip() c1 = list() for c in text: if c.isdigit(): c1 += c else: c1 += " " try: num = float("".join(c1).split()[0]) except: num = None return num <type return> >>> GetNum("10-Inches Bituminous Concrete 8-Inches Portland Cement ") 10.0
I want to parse a text field into a floating point field. (e.g.: "10-Inches Bituminous Concrete 8-Inches Portland Cement Concrete 6-Inches Crushed Stone Base" becomes "10")
>>> text = "10 asdf 12" >>> [c for c in text if c.isdigit() or c.isspace()] ['1', '0', ' ', ' ', '1', '2'] >>> "".join([c for c in text if c.isdigit() or c.isspace()]) '10 12' >>>
def GetNum(text): text = text.strip() text = "".join([c for c in text if c.isdigit() or c.isspace()]) try: num = float(text.split()[0]) except: num = None return num
def GetNum(text): text = text.strip() c1 = list() for c in text: if c.isdigit(): c1 += c else: c1 += " " try: num = float("".join(c1).split()[0]) except: num = None return num
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.