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

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


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.
>  
> thanks
> tcl76
>  
> ####sampledata############
> Platform: PC
> Tempt : 25
> TAP0 :0
> TAP1 :1
> 
> +++++++++++++++++++++++++++++++++++++++++++++
> Port Chnl Lane EyVt
> +++++++++++++++++++++++++++++++++++++++++++++
> 0       1      1    75 
> 0       1      2    -1
> 0       1      3    10
> 0       1      4    -1
> 0       1      5    12
> +++++++++++++++++++++++++++++++++++++++++++++
> Time: 20s
> ############################
>  
> ####output###################
> case1: Found -1 
> Lane 2 FAIL 
> Lane 4 FAIL
>  
> case2: No -1 
> All Lanes PASS
> ##########################
>  
> ####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"
 > for search in open(fname):
 >     if pattern in search:
 >         print "FAIL"
 >     else:
 >         print "PASS"



> 
>   		 	   		  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list