Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'defaults': 0.07; 'result,': 0.07; 'string': 0.09; 'input,': 0.09; 'parameter': 0.09; 'underscore': 0.09; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; '(%s)"': 0.16; '-tkc': 0.16; 'expected,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'optional': 0.16; 'splits': 0.16; 'wrote:': 0.18; 'split': 0.19; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; 'skip:( 20': 0.30; 'work.': 0.31; 'but': 0.35; 'charset:us-ascii': 0.36; 'should': 0.36; 'expected': 0.38; 'to:addr:gmail.com': 0.65; 'received:50.22': 0.84; '"how': 0.91 Date: Thu, 16 May 2013 10:23:42 -0500 From: Tim Chase To: loial Subject: Re: spilt question In-Reply-To: References: X-Mailer: Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: python-list@python.org 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368717707 news.xs4all.nl 15894 [2001:888:2000:d::a6]:41081 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45435 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