Replace string with values in a new field column ?

3602
15
01-19-2016 12:41 PM
PROBERT68
Frequent Contributor

I have a Forest roads here with me and there is a column (iD ) that shows street name number, freeway, state, county along with their numbers..

I copied them to a new field column Symbol that I need to calculate them with just numbers only.

This purpose is to show them the road symbol number as a labels

How do I do that  in the Field Calculator ?ID.png

0 Kudos
15 Replies
DanPatterson_Retired
MVP Emeritus

as I suggested before

python of course

>>> a ="83N"

>>> b = "".join([i for i in a if i in '0123456789'])

>>> b

'83'

where 'a' is !yourfield!

where a is your field and b will be your new field which you don't have to specify.  This could be used directly in the field calculator

AND

it will not work in all cases as I stated before since I doubt that highway 360.2B is going to come out as 360.2 but 3602.

multiple steps may be necessary.

If you want a numeric value, wrap the whole thing in an int( ) statement

DarrenWiens2
MVP Honored Contributor

Where exactly is the number in "MOR-Q"? Does this have what you call a number, or is there no number for this one?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Can account for it... but it is getting less elegant

>>> a = "MOR-Q"
>>> b = "".join([i for i in a if i in '0123456789'])
>>> b
''

or for the devil's highway in nodata form

>>> b = ["".join([i for i in a if i in '0123456789']) or -666][0]
>>> b
-666
ChrisDonohue__GISP
MVP Alum

Wait, Dan, what happened to the smoking one-liner (of code)?   

(just kidding)

Chris Donohue, GISP

DanPatterson_Retired
MVP Emeritus

There are probably some in the long history of this question...  How do you write expression in Label field to replace string with values ?  Nothing seems to work, I think the questions are asked and forgotten.

TedKowal
Occasional Contributor III

Sorry -- I just could not resist ..... Temptation got the better of me! 

int(filter(str.isdigit, [ID]))

BTW:  I had an access denied when trying to use the advanced editor....

DanPatterson_Retired
MVP Emeritus

trying to cut down on filter, map and lambda... beginning to forget their usefulness

0 Kudos
PROBERT68
Frequent Contributor

VBScript or Python ?

Tried and gave me the error

0 Kudos
TedKowal
Occasional Contributor III

If you want to try vbscript:

int([ID])

Of course those string with no numbers will come out to be 0.

0 Kudos