[Tutor] this is ugly

Alan Gauld alan.gauld at blueyonder.co.uk
Tue Oct 21 17:31:01 EDT 2003


> Can line 3 of the following be improved (especially since I might
want
> to search for even more strings within each line):
>
> myfile=open(r'F:\logs\myfile.txt', 'r')
> for line in myfile.readlines():
>    if line.find("KeC 923") != -1 or line.find("ZEF 156") != -1 or
> line.find("pBX 88347") != -1 or line.find("FZX 17255") != -1:
>          #code if found

Yes use a regular expression. You can combine conditions in one
expression and it will only search the string once instead of
once per condition.

I'll leave it to someone else to explain how to combine the
regexs! I'm going to bed and my brain is tired.... :-)

Alan G.




More information about the Tutor mailing list