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


Groups > comp.lang.python > #45435

Re: spilt question

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 <python.list@tim.thechases.com>
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 <python.list@tim.thechases.com>
To loial <jldunn2000@gmail.com>
Subject Re: spilt question
In-Reply-To <d8c03de0-dc35-45e3-a6b2-2af39feb9e79@googlegroups.com>
References <d8c03de0-dc35-45e3-a6b2-2af39feb9e79@googlegroups.com>
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 <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.1760.1368717707.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


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


Back to comp.lang.python | Previous | NextPrevious in thread | Next 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