spilt question

Ned Batchelder ned at nedbatchelder.com
Thu May 16 11:22:15 EDT 2013


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130516/1fa27548/attachment.html>


More information about the Python-list mailing list