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: 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 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: In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 05/16/2013 11:15 AM, Chris Angelico wrote: > On Fri, May 17, 2013 at 1: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 > > 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