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

tee chwee liong tcl76 at hotmail.com
Sun Jan 9 15:19:09 CET 2011


thanks this solve the error but running the code produces another error ie index out of range.
Traceback (most recent call last):
  File "C:\Python25\myscript\log\readfile9.py", line 22, in <module>
    if line[3] == pattern:
IndexError: list index out of range
 
> To: tutor at python.org
> From: alan.gauld at btinternet.com
> Date: Sun, 9 Jan 2011 14:07:39 +0000
> Subject: Re: [Tutor] Open a text file, read and print pattern matching
> 
> 
> "tee chwee liong" <tcl76 at hotmail.com> wrote
> 
> > i got syntax error when running this line:
> > data = for line in open(filename) if not line.startswith("#")
> 
> Should be:
> 
> data = [ line for line in open(filename) if not line.startswith("#") ]
> 
> Notice the extra line at the beginning and the fact that it is
> surrounded by [] which makes it a *list comprehension*.
> 
> 
> But that won't help if you can't put commment markers
> at the front... But you might get away with:
> 
> data = [ line for line in open(filename) if '-1' in line ]
> 
> HTH,
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110109/23f34b69/attachment.html>


More information about the Tutor mailing list