Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

spilt question

20 views
Skip to first unread message

loial

unread,
May 16, 2013, 11:00:25 AM5/16/13
to
I want to split a string so that I always return everything BEFORE the LAST underscore

HELLO_xxxxxxxx.lst # should return HELLO
HELLO_GOODBYE_xxxxxxxx.ls # should return HELLO_GOODBYE

I have tried with rsplit but cannot get it to work.

Any help appreciated

Walter Hurry

unread,
May 16, 2013, 11:10:14 AM5/16/13
to
mystr.rsplit("_",1)[0]

Fábio Santos

unread,
May 16, 2013, 11:14:27 AM5/16/13
to loial, pytho...@python.org

str.split takes a limit argument. Try your_string.split('_', 1)

Chris Angelico

unread,
May 16, 2013, 11:15:39 AM5/16/13
to pytho...@python.org
Try with a limit:

>>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1)
['HELLO_GOODBYE', 'xxxxxxxx.ls']
>>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1)[0]
'HELLO_GOODBYE'

You can easily get docs on it:

>>> help("".rsplit)
Help on built-in function rsplit:

rsplit(...)
S.rsplit(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string, starting at the end of the string and
working to the front. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified, any whitespace string
is a separator.

ChrisA

Dave Angel

unread,
May 16, 2013, 11:20:25 AM5/16/13
to pytho...@python.org
The rsplit() method was a good idea; it will work. So exactly what did
you try and what about it did not work. How are you stuck going from
your partial answer to a complete one?

While you're at it, you need to consider at least two more cases

HELLO.txt #should return ""
HELLO_and_____goodbye #should return "HELLO_and____"

--
DaveA

Tim Chase

unread,
May 16, 2013, 11:23:42 AM5/16/13
to loial, pytho...@python.org
.rsplit takes an optional "how many splits do you want?" parameter
that defaults to giving you all of them. Just ask for one
right-most split:

TESTS = [
("HELLO_xxxxxxx.lst", "HELLO"),
("HELLO_GOODBYE_xxxxx.ls", "HELLO_GOODBYE"),
]

for input, expected in TESTS:
result = input.rsplit('_', 1)[0]
if result == expected:
verdict = "passed"
else:
verdict = "failed"
print "%r -> %r == %r (%s)" % (
input,
result,
expected,
verdict,
)

-tkc



Ned Batchelder

unread,
May 16, 2013, 11:22:15 AM5/16/13
to loial, pytho...@python.org
>>> "HELLO_GOODBYE_xxxxxxxx.ls".rpartition('_')
('HELLO_GOODBYE', '_', 'xxxxxxxx.ls')
>>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit('_', 1)
['HELLO_GOODBYE', 'xxxxxxxx.ls']
>>> "HELLO_GOODBYE_xxxxxxxx.ls".rpartition('_')[0]
'HELLO_GOODBYE'
>>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit('_', 1)[0]
'HELLO_GOODBYE'

For the future, getting help works better if you show what you tried, and what it produced.

--Ned.

Dave Angel

unread,
May 16, 2013, 11:32:36 AM5/16/13
to pytho...@python.org
On 05/16/2013 11:15 AM, Chris Angelico wrote:
> On Fri, May 17, 2013 at 1:00 AM, loial <jldun...@gmail.com> wrote:
> Try with a limit:
>
>>>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1)
> ['HELLO_GOODBYE', 'xxxxxxxx.ls']
>>>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1)[0]
> 'HELLO_GOODBYE'
>
> You can easily get docs on it:
>
>>>> help("".rsplit)
> Help on built-in function rsplit:
>
> rsplit(...)
> S.rsplit(sep=None, maxsplit=-1) -> list of strings
>
> Return a list of the words in S, using sep as the
> delimiter string, starting at the end of the string and
> working to the front. If maxsplit is given, at most maxsplit
> splits are done. If sep is not specified, any whitespace string
> is a separator.
>
> ChrisA
>

Now that you've all done his homework for him, see if The OP can spot
the one case where that won't work. It's easy to test for, but still
important to get right (for some definition of right).

--
DaveA
0 new messages