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.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to