scanf style parsing

Grant Edwards grante at visi.com
Sat Sep 29 12:06:23 EDT 2001


On 29 Sep 2001 15:47:49 GMT, chasm at atlantis.rift <chasm at atlantis.rift> wrote:
>>> Regex's are useful and powerful.  But they're also very easy to
>>> abuse.  I've actually seen the following Perl code:
>>> 
>>>     if ($filename =~ /\.txt$/) { ... }
>>> ...
>>>
>>> or, much more preferably:
>>> 
>>>     if filename[-4:] == '.txt':
>>
>>Overall, the Perl code's better.  It didn't have to hard-code the
>>length of the string.
>
>	ext = '.txt'
>	if filename[-len(ext):] == ext:

  if filename.endswith(".txt"):

-- 
Grant Edwards                   grante             Yow!  I have many CHARTS
                                  at               and DIAGRAMS...
                               visi.com            



More information about the Python-list mailing list