[Tutor] Open a text file, read and print pattern matching

Steven D'Aprano steve at pearwood.info
Sun Jan 9 07:44:58 CET 2011


Please excuse the double post, I had a problem with my email program.

tee chwee liong wrote:
> hi, 
> 
> i have a sampledata as below. Pls refer to output, if found -1, how to list out all the Lane number? And if there is no -1, print PASS. My code is as below section.

> ####Code####################
> fname = "sampledata.txt" 
> pattern = "-1"
> for search in open(fname):
>     if pattern in search:
>         print "FAIL"
>     else:
>         print "PASS"

fname = "sampledata.txt"
pattern = "-1"
failed = False
for line in open(fname):
     port, channel, lane, eyvt = line.split()
     if int(eyvt) == -1:
         failed = True
         print "Lane %s failed." % lane
if not failed:
     print "All lanes pass."


-- 
Steven



More information about the Tutor mailing list