Reading a file

Aahz aahz at pythoncraft.com
Thu Mar 5 05:41:53 CET 2009


In article <mailman.9538.1234633556.3487.python-list at python.org>,
Terry Reedy  <tjreedy 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=')
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"All problems in computer science can be solved by another level of     
indirection."  --Butler Lampson



More information about the Python-list mailing list