Reading a file

John Machin sjmachin at lexicon.net
Thu Mar 5 22:42:34 CET 2009


On Mar 6, 1:28 am, MRAB <goo... at mrabarnett.plus.com> wrote:
> Aahz wrote:
> > In article <mailman.9538.1234633556.3487.python-l... at python.org>,
> > Terry Reedy  <tjre... at udel.edu> wrote:
> >> for line in open('char.txt'):
> >>   if line.find('sweet') != -1 or line.find('blue') != -1:
> >>     print(line)
>
> > For any recent Python, this should be:
>
> >     if 'sweet' in line or 'blue' in line:
>
> > Although I think that for the OP's use case, it ought to be:
>
> >     if line.startswith('sweet=') or line.startswith('blue=')
>
> Or:
>
>      if line.startswith(('sweet=', 'blue=')):

C'est magnifique mais ce n'est pas la guerre ... c'est "bassackwards"
** 2:

(1) 'Mango' is the query, 'sweet' is the desired result
(2) The line, after stripping \n, *ends* with 'sweet'

And it's not robust in the face of likely whitespace either side of
the '='



More information about the Python-list mailing list