field calculator - copying field without last letter/number

2259
12
Jump to solution
06-25-2015 08:16 AM
MassimoSpirlandelli
New Contributor II

I have to copy a field (text) into another field (text) except the last letter/number.

For istance:

in field "A" with attribute  "1111A"

I want to get just 1111 in field "B"

how can I do it?

thank you in advance.

0 Kudos
1 Solution

Accepted Solutions
SepheFox
Frequent Contributor

Hi Massimo, in the field calculator, choose the Python parser, and type:

!A![:-1]

View solution in original post

12 Replies
DanPatterson_Retired
MVP Esteemed Contributor

"1111A" is the value

In python for example

>>> a = "1111A"

>>> b = a[:-1]

>>> b

'1111'

>>>

so in the field calculator, you would setup a new field probably text if you can't guarantee all the slicing will be numeric, then use the field calculator syntax with the python parser selected a

oldfield[:-1]

the : means everything up to and -1 means the last one (counting backward from the end.  There is a whole section on field calculations in the online help files... ie from the 10.3 help   Fundamentals of field calculations—Help | ArcGIS for Desktop

MassimoSpirlandelli
New Contributor II

thank you

0 Kudos
SepheFox
Frequent Contributor

Hi Massimo, in the field calculator, choose the Python parser, and type:

!A![:-1]

MassimoSpirlandelli
New Contributor II

thank you

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

Nice one Sephe!!! Good to know...

I got a longer one though

Left( ["FieldName"], Len( ["FieldName"] ) - 1 )



Think Location
SepheFox
Frequent Contributor

It would be fun to have a competition to see who could write the longest piece of code to simply strip one character off the end of a  field!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

"".join([a for i in range(len(a)-1)])

where a = !yourfieldname!

SepheFox
Frequent Contributor

Haha! Dan takes the lead!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

>>> ("{}"*(len(a)-1)).format(*a)

'1111'

enough...Or I will bring out numpy examples