Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45428 > unrolled thread
| Started by | loial <jldunn2000@gmail.com> |
|---|---|
| First post | 2013-05-16 08:00 -0700 |
| Last post | 2013-05-16 11:32 -0400 |
| Articles | 8 — 7 participants |
Back to article view | Back to comp.lang.python
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
| From | loial <jldunn2000@gmail.com> |
|---|---|
| Date | 2013-05-16 08:00 -0700 |
| Subject | spilt question |
| Message-ID | <d8c03de0-dc35-45e3-a6b2-2af39feb9e79@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Walter Hurry <walterhurry@lavabit.com> |
|---|---|
| Date | 2013-05-16 15:10 +0000 |
| Message-ID | <kn2ssm$rqg$1@news.albasani.net> |
| In reply to | #45428 |
On Thu, 16 May 2013 08:00:25 -0700, loial 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.
>
mystr.rsplit("_",1)[0]
[toc] | [prev] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-05-16 16:14 +0100 |
| Message-ID | <mailman.1756.1368717275.3114.python-list@python.org> |
| In reply to | #45428 |
[Multipart message — attachments visible in raw view] — view raw
str.split takes a limit argument. Try your_string.split('_', 1)
On 16 May 2013 16:11, "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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-05-17 01:15 +1000 |
| Message-ID | <mailman.1757.1368717347.3114.python-list@python.org> |
| In reply to | #45428 |
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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-05-16 11:20 -0400 |
| Message-ID | <mailman.1759.1368717643.3114.python-list@python.org> |
| In reply to | #45428 |
On 05/16/2013 11:00 AM, loial 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 > 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
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-05-16 10:23 -0500 |
| Message-ID | <mailman.1760.1368717707.3114.python-list@python.org> |
| In reply to | #45428 |
On 2013-05-16 08:00, loial 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.
.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
[toc] | [prev] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2013-05-16 11:22 -0400 |
| Message-ID | <mailman.1761.1368717742.3114.python-list@python.org> |
| In reply to | #45428 |
[Multipart message — attachments visible in raw view] — view raw
On 5/16/2013 11:00 AM, loial 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
>
>>> "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.
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-05-16 11:32 -0400 |
| Message-ID | <mailman.1762.1368718369.3114.python-list@python.org> |
| In reply to | #45428 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web