Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed1.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'string': 0.09; 'underscore': 0.09; 'subject:question': 0.10; 'cc:addr:python- list': 0.11; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'wrote:': 0.18; 'split': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; '>>>': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'appreciated': 0.26; 'skip:" 30': 0.26; 'skip:" 40': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'am,': 0.29; 'work.': 0.31; 'getting': 0.31; "skip:' 10": 0.31; 'but': 0.35; 'should': 0.36; 'skip:[ 10': 0.38; 'show': 0.63; 'to:addr:gmail.com': 0.65; 'future,': 0.83 Date: Thu, 16 May 2013 11:22:15 -0400 From: Ned Batchelder User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: loial Subject: Re: spilt question References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------010204000303020303000900" 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: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368717742 news.xs4all.nl 15966 [2001:888:2000:d::a6]:41851 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45436 This is a multi-part message in MIME format. --------------010204000303020303000900 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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. --------------010204000303020303000900 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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.
--------------010204000303020303000900--