spilt question
Chris Angelico
rosuav at gmail.com
Thu May 16 11:15:39 EDT 2013
On Fri, May 17, 2013 at 1:00 AM, loial <jldunn2000 at gmail.com> 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
Try with a limit:
>>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1)
['HELLO_GOODBYE', 'xxxxxxxx.ls']
>>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1)[0]
'HELLO_GOODBYE'
You can easily get docs on it:
>>> help("".rsplit)
Help on built-in function rsplit:
rsplit(...)
S.rsplit(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string, starting at the end of the string and
working to the front. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified, any whitespace string
is a separator.
ChrisA
More information about the Python-list
mailing list