Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45437

Re: spilt question

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.056
X-Spam-Evidence '*H*': 0.89; '*S*': 0.00; 'string': 0.09; 'specified,': 0.09; 'underscore': 0.09; 'subject:question': 0.10; 'delimiter': 0.16; 'for,': 0.16; 'given,': 0.16; 'received:74.208.4.195': 0.16; 'separator.': 0.16; 'splits': 0.16; 'wrote:': 0.18; 'split': 0.19; 'header:User-Agent:1': 0.23; 'string,': 0.24; 'appreciated': 0.26; 'skip:" 40': 0.26; '(for': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'words': 0.29; 'work.': 0.31; "skip:' 10": 0.31; '>>>>': 0.31; 'sep': 0.31; 'fri,': 0.33; 'definition': 0.35; 'done.': 0.35; 'test': 0.35; 'but': 0.35; 'done': 0.36; 'should': 0.36; 'list': 0.37; 'easily': 0.37; 'starting': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'easy': 0.60; 'most': 0.60; "you've": 0.63; 'him,': 0.64; 'spot': 0.65; 'received:74.208': 0.68; '1:00': 0.84; 'front.': 0.84; 'homework': 0.84; 'right).': 0.84; '2013': 0.98
Date Thu, 16 May 2013 11:32:36 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5
MIME-Version 1.0
To python-list@python.org
Subject Re: spilt question
References <d8c03de0-dc35-45e3-a6b2-2af39feb9e79@googlegroups.com> <CAPTjJmoqD_gj25Pvo-cP30A_Ujc=7sVHp-xV=G8U6zbQbTk5Pw@mail.gmail.com>
In-Reply-To <CAPTjJmoqD_gj25Pvo-cP30A_Ujc=7sVHp-xV=G8U6zbQbTk5Pw@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:jbabMTRaXQ1g4j/90tNY6BUmHtRfvqIfZcBmM3t+dn2 mtN5GCk3ZtJxPAz/Hla3tVR++y6SP7pp5Q3H8heRKh0YNN+64a lSU1e1tjFiRdMS8h4Pn+KmQCWR4JAjBzEsvjMuWKyYCoLO8vcc IPWXwRsCWkp+yoZwZVtUyMlbd0iL7uaV/6jYrLHYJwrCc4Re6S b9T0lEayn8ONmMKJLIF29ViV234NHnmZ3VUP83pIY7/z2W8zv6 XJh9mEGkYQauDm94Qt1HDE5Sh1DPw4v3RAMPihTT4YsLjGln+v zmU/EEJrBA/HPtFHB4/DQMgrCnAzW8xFBgpQk5GPn9WpmQ7gg= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1762.1368718369.3114.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1368718369 news.xs4all.nl 15910 [2001:888:2000:d::a6]:55465
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45437

Show key headers only | View raw


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 | NextPrevious in thread | Find similar | Unroll thread


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