Select to view content in your preferred language

Removing parts of a string field python calculator

12152
7
08-29-2014 12:30 PM
StephenNorris
Deactivated User

whats the syntax for removing a specific number of characters from a string.

for example "name.pdf" I want to remove from the right the ".pdf"

Sorry this is a basic question. my searches aren't finding the answer.

Thanks,

Steve

7 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Stephen,

You can use the split method.  Ex:

txt = "name.pdf"

txt.split(".")[0]

This will return 'name'.

StephenNorris
Deactivated User

I'll try that one too. I actually used a successful search strip and found another answer.

!Field_name! .rstrip(".pdf")

StephenNorris
Deactivated User

Also I was using the field calculator in arcmap. in case that changes anything

0 Kudos
StephenNorris
Deactivated User

Thanks for the replies everyone. much appreciated

0 Kudos
DanPatterson_Retired
MVP Emeritus

or a little more cryptic

>>> txt = "name.pdf"

>>> a = txt[:-4]  #strip off last 4 characters

>>> a

'name'

>>>

the [  ] means that text is whats called an iterable, meaning each character can be cycled through

the [:   means copy everything from the beginning (note the colon after the [ )

the -4] means everything except the last 4 characters (counting from the right)

hence it all means copy the "txt" variable (name.pdf) except the last 4 characters

MatthiasAbele
Emerging Contributor

Hi Stephen,

it would be easier to to it with Excel using the GISconnector.

In Excel, use search/replace or strip the characters with =right(), =left()...

Best

Matthias

0 Kudos
DanPatterson_Retired
MVP Emeritus

Well whatever Excel can do python can do as well

>>> txt = "name.pdf"

>>> stripped = txt.rstrip(".pdf")

>>> stripped

'name'

>>>

0 Kudos