Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

truncate trailing digits from string without knowing number of trailing digits?

$
0
0
What's the most elegant way to truncate trailing digits from a string without knowing beforehand how many trailing digits you will encounter.
In particular, I want to be careful not to truncate digits contained within a given string's basename or those that a prefixed, only the suffix values that are digits.

Example:

"7-Eleven" : I don't want to truncate anything.
"7-Eleven 2" : I want to truncate the '2'
"7-Eleven 2364" : I want to truncate the '2364'.

That last part if a bit of an exaggeration, I have exceptions in place to keep things from getting crazy, but there could be instances where I don't know how many digits to expect beforehand and need to truncate them from the end of the string.

Thus far, I gotten to something along the lines of:
Code:

if str(MyLayer.name[-1]).isDigit():
    newName = str(MyLayer.name[:-1])
elif str(MyLayer.name[-2:].isDigit():
    newName = str(MyLayer.name[:-2])

You can see how this could go on and on...
I'm looking for some sort of dynamic method to determine the number of trailing characters I need to drop in order to remove all of the trailing digits from the Layer Name.

Viewing all articles
Browse latest Browse all 2485

Trending Articles