Replace string with values in a new field column ?

2748
15
01-19-2016 12:41 PM
Highlighted
Regular Contributor II

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

Reply
0 Kudos
15 Replies
Highlighted
MVP Esteemed Contributor

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

Highlighted
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?

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

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

Highlighted
MVP Frequent Contributor

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

(just kidding)

Chris Donohue, GISP

Highlighted
MVP Esteemed Contributor

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.

Highlighted
Regular Contributor II

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....

Highlighted
MVP Esteemed Contributor

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

Reply
0 Kudos
Highlighted
Regular Contributor II

VBScript or Python ?

Tried and gave me the error

Reply
0 Kudos
Highlighted
Regular Contributor II

If you want to try vbscript:

int([ID])

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

Reply
0 Kudos