Field Calc Integers from String

804
7
01-16-2023 07:29 AM
Labels (3)
JeremiahMcDonald
New Contributor II

I am trying to Field Calc the integers that live in a STRING field for roads. The client only wants the numbers of each road and not any of the text. I have tried : def comp(x):
return any(i.isdigit() for i in x)

No real results. This finds a number and returns "1" if true and "0" if false. What I need is for the entire number, say 100th Street, would become 100. Any help would be much appreciated. 

 

JeremiahMcDonald_0-1673882958334.png

 

Sincerely,
Jeremiah McDonald
0 Kudos
7 Replies
JoshuaBixby
MVP Esteemed Contributor

Several ways to go about it depending on how clean and/or complicated the data are in text fields.  You can use itertools.takewhile() or just adopt the pseudo-code from it.

0 Kudos
JeremiahMcDonald
New Contributor II

can you provide an example with the snippet given?

Sincerely,
Jeremiah McDonald
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Using the pseudo-code directly from page I link to:

# Codeblock
def takewhile(predicate, iterable):
    for x in iterable:
        if predicate(x):
            yield x
        else:
            break

# Expression 
"".join(takewhile(str.isdigit, !FULLNAME!))
DanPatterson
MVP Esteemed Contributor

"any" is used to return a boolean, in this case 1/True that there was a number in the field.

If you want the number then

a = '100 some street'

"".join([i for i in a if i.isdigit()])
'100'

It will continue to return numbers whether they make sense or not like

a = "100 1st ave apt 5"

will return "10015"

which would require some extra work to just get the street address

a = "100 1st ave apt 5"
# -- getting there
"".join([i for i in a if i.isdigit() or i == " "])
'100 1   5'
# -- split the string and slice the part you want
"".join([i for i in a if i.isdigit() or i == " "]).split(" ")[0]
Out[5]: '100'

So examine and modify your use case to avoid such things


... sort of retired...
JayantaPoddar
MVP Esteemed Contributor

Another one

Code Block

 

def comp(x):
    numlist = [i for i in x if i.isdigit()]
    if len(numlist)>0:
        return int(''.join(numlist))

 

 

Expression:

 

comp( !NUMBER_STRING! )

https://gis.stackexchange.com/questions/364715/extracting-numbers-from-string-using-field-calculator...

 

 



Think Location
JoshuaBixby
MVP Esteemed Contributor

As Dan already mentioned, this general approach will fail when there are other number sequences in the string.  For example, an address like "100 South 900 West" will return "100900" with this approach.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

This is another good use case for my Idea for Esri to Add Python Regular Expression (re) Helpers to Calculate Field Dialog .  Even without the "Helper" functions, you can still use re - Regular expression operations - Python 3 documentation by just importing the module in the code block:

# Codeblock
import re

# Expression
re.match(r"^\s*\d*", !FULLNAME!).group()
0 Kudos