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



-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to