Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45437
| Date | 2013-05-16 11:32 -0400 |
|---|---|
| From | Dave Angel <davea@davea.name> |
| Subject | Re: spilt question |
| References | <d8c03de0-dc35-45e3-a6b2-2af39feb9e79@googlegroups.com> <CAPTjJmoqD_gj25Pvo-cP30A_Ujc=7sVHp-xV=G8U6zbQbTk5Pw@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1762.1368718369.3114.python-list@python.org> (permalink) |
On 05/16/2013 11:15 AM, Chris Angelico wrote:
> On Fri, May 17, 2013 at 1:00 AM, loial <jldunn2000@gmail.com> wrote:
>> 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
>
> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
spilt question loial <jldunn2000@gmail.com> - 2013-05-16 08:00 -0700 Re: spilt question Walter Hurry <walterhurry@lavabit.com> - 2013-05-16 15:10 +0000 Re: spilt question Fábio Santos <fabiosantosart@gmail.com> - 2013-05-16 16:14 +0100 Re: spilt question Chris Angelico <rosuav@gmail.com> - 2013-05-17 01:15 +1000 Re: spilt question Dave Angel <davea@davea.name> - 2013-05-16 11:20 -0400 Re: spilt question Tim Chase <python.list@tim.thechases.com> - 2013-05-16 10:23 -0500 Re: spilt question Ned Batchelder <ned@nedbatchelder.com> - 2013-05-16 11:22 -0400 Re: spilt question Dave Angel <davea@davea.name> - 2013-05-16 11:32 -0400
csiph-web