can it be shorter?
nick at craig-wood.com
Sat Jun 6 20:29:41 CEST 2009
tsangpo <tsangpo.newsgroup at gmail.com> wrote:
> "kj" <no.email at please.post> 写入消息 news:h0e3p9$85t$1 at reader1.panix.com...
> > In <h0e0oi$1es2$1 at adenine.netfront.net> "tsangpo"
> > <tsangpo.newsgroup at gmail.com> writes:
> >>I want to ensure that the url ends with a '/', now I have to do thisa like
> >>url = url + '' if url[-1] == '/' else '/'
> >>Is there a better way?
> > It's a pity that in python regexes are an "extra", as it were.
> > Otherwise I'd propose:
> > url = re.sub("/?$", "/", url)
> > kynn (lowest-of-the-low python noob)
> look nice, but:
> >>> re.sub('/?$/', '/', 'aaabbb')
> has no effect. what a pity.
That is because you typoed what kynn wrote.
>>> re.sub('/?$', '/', 'aaabbb')
That solution is very perl-ish I'd say, IMHO
if not url.endswith("/"):
url += "/"
is much more pythonic and immediately readable. In fact even someone
who doesn't know python could understand what it does, unlike the
regexp solution which requires a little bit of thought.
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list