Re: scanf style parsing
by Stefan Schwarzer other posts by this author
Sep 29 2001 3:05PM messages near this date
Re: scanf style parsing
|
Re: scanf style parsing
Hello Tim
Tim Hammerquist wrote:
> > > or, much more preferably:
> > >
> > > if filename[-4:] == '.txt':
> > > ...
> >
> > Since (I think:) Python 2.0 it's possible to use
> >
> > if filename.endswith('.txt'):
> > ...
> >
> > which is less error-prone if the string is a bit longer.
>
> ..and much more maintainable. You no longer must change the index when
> the length of the string changes.
Yes, I meant that by "less error-prone". Up to four characters are yet
_relatively_ simply to see, but with more characters it is easy to mismatch
the string and the number.
Because endswith exists, this is the better solution, of course. But otherwise,
it could anyhow also be defined like
def endswith(s, part):
return s[ -len(part) : ] == part
> Very nice. I hadn't been
> familiarized with this method. Thank you for bringing it up.
:-)
Stefan
--
http://mail.python.org/mailman/listinfo/python-list
Thread:
Bruce Dawson
Skip Montanaro
George Demmy
Hans-Peter Jansen
Quinn Dunkan
Tim Hammerquist
Ralph Corderoy
Toby Dickenson
Duncan Booth
Aahz Maruch
Aahz Maruch
Aahz Maruch
Stefan Schwarzer
Grant Edwards
Fredrik Lundh
Malcolm Tredinnick
Ralph Corderoy
Tim Hammerquist
Stefan Schwarzer
Greg Ewing
Skip Montanaro
Boyd Roberts
Steve Clift
Bruce Dawson
Tim Hammerquist
Tim Hammerquist
Tim Hammerquist
Skip Montanaro
Andrew Dalke
Fredrik Lundh
Oleg Broytmann
Andrei Kulakov
Duncan Booth
Chris Barker
Tim Hammerquist
Skip Montanaro
Jon Nicoll
Oleg Broytmann
Bruce Dawson
Skip Montanaro
Andrei Kulakov
Richard Jones
Skip Montanaro
Andrew Dalke
|