[Tutor] url parsing

Kent Johnson kent37 at tds.net
Sun Feb 15 15:30:59 CET 2009


On Sun, Feb 15, 2009 at 5:09 AM, Jay Jesus Amorin <jay.amorin at gmail.com> wrote:
> Hi,
>
> Can you please help my how to parse.
>
> url = http://this/is/my/url/to/parse
>
> how do i parse url to print "http://this/is/my/url/to"
>
> i want to remove the last part of my url with is "parse". I would like to
> remove the last string of my url.
>
> i have try split, but i think it wont work as i dont know how long my url
> would be in my real application.

If you just want to remove everything after the last / then rsplit()
with an argument telling it how many times to split will do it:

In [13]: url = 'http://this/is/my/url/to/parse'

In [14]: url.rsplit('/', 1)
Out[14]: ['http://this/is/my/url/to', 'parse']

In [15]: url.rsplit('/', 1)[0]
Out[15]: 'http://this/is/my/url/to'

Kent


More information about the Tutor mailing list