Separate Numbers from Text

218
6
Jump to solution
2 weeks ago
ChrisMacNeillNCFPD
New Contributor II

Hey team, I have an address field with the entire address in one cell, which looks like this one for example "15775 W California AVE ".  I'm trying to pull out just the number portion so I can join it to another table.  The format is inconsistent and some are 3 digits and some are 5 so its giving me more trouble than I was anticipating.  Anyone know of a way to do this with a calculated field?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor
a = "15775 W California AVE"

"".join([i for i in a.split(" ")[0] if i.isdigit])
'15775'  # for text output

int("".join([i for i in a.split(" ")[0] if i.isdigit]))
15775 # for integer output

... sort of retired...

View solution in original post

6 Replies
DanPatterson
MVP Esteemed Contributor
a = "15775 W California AVE"

"".join([i for i in a.split(" ")[0] if i.isdigit])
'15775'  # for text output

int("".join([i for i in a.split(" ")[0] if i.isdigit]))
15775 # for integer output

... sort of retired...
ChrisMacNeillNCFPD
New Contributor II

Awesome, thank you!

0 Kudos
ChrisMacNeillNCFPD
New Contributor II

Would I replace the actual address in line 1 with the field name? So in this case a = !Address!

0 Kudos
DanPatterson
MVP Esteemed Contributor

Yes, for field calculations.  In my example  a  would be replaced by !YourFieldName! ( !Address!, if that is the fieldname)


... sort of retired...
0 Kudos
JasonBagwell1
New Contributor III

Hi Chris,

I've used this Python script/calculation before with success.

!YourField!.split(' ')[0]

This separates the field at the first space, returning just the address number.

 

DanPatterson
MVP Esteemed Contributor

split works unless the number has a letter attached like, hence the rest of the code

15775a

... sort of retired...