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


Groups > comp.lang.python > #45435

Re: spilt question

Date 2013-05-16 10:23 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: spilt question
References <d8c03de0-dc35-45e3-a6b2-2af39feb9e79@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1760.1368717707.3114.python-list@python.org> (permalink)

Show all headers | 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