[Tutor] Equivalent of grep in python

Tiago Katcipis katcipis at inf.ufsc.br
Sun Dec 21 20:14:00 CET 2008


Sorry its true, i made a mistake. Readlines is a list with all the lines
inside. I never used readlines (i usually use read), i just read about it on
the tutorial long time ago.

>>> f.readlines()
['This is the first line of the file.\n', 'Second line of the file\n'


Thanks for the help.


On Sun, Dec 21, 2008 at 4:44 PM, Luke Paireepinart
<rabidpoobear at gmail.com>wrote:

> I believe readlines returns a list of strings, not a list of lists.
> You can iterate over the characters in a string if you want, though.
>
> On 12/21/08, Tiago Katcipis <katcipis at inf.ufsc.br> wrote:
> > i forgot, this might help you
> >
> >
> http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
> >
> >
> >  On Sun, Dec 21, 2008 at 11:57 AM, Tiago Katcipis <katcipis at inf.ufsc.br>
> > wrote:
> > > i believe that the following should work
> > >
> > > file1 = open(fileO, 'r')
> > > re.findall ('some_text', file1.read())
> > >
> > > readlines returns a list with lists inside, where every list is a line
> of
> > the text. The read function returns the entire file as one string, so it
> > should work to what you are wanting to do.
> > >
> > > best regards
> > >
> > > Katcipis
> > >
> > >
> > >
> > >
> > >
> > > On Sun, Dec 21, 2008 at 12:02 AM, ppaarrkk <simon_ecc at yahoo.co.uk>
> wrote:
> > >
> > > >
> > > > The following works :
> > > >
> > > > file1 = open (file0, "r")
> > > >
> > > > re.findall ( 'some_text', file1.readline() )
> > > >
> > > >
> > > > But this doesn't :
> > > >
> > > > re.findall ( 'some_text', file1.readlines() )
> > > >
> > > >
> > > >
> > > > How do I use grep for a whole text file, not just a single string ?
> > > > --
> > > > View this message in context:
> >
> http://www.nabble.com/Equivalent-of-grep-in-python-tp21111356p21111356.html
> > > > Sent from the Python - tutor mailing list archive at Nabble.com.
> > > >
> > > > _______________________________________________
> > > > Tutor maillist  -  Tutor at python.org
> > > > http://mail.python.org/mailman/listinfo/tutor
> > > >
> > >
> > >
> > >
> > > --
> > > "it might be a profitable thing to learn Java, but it has no
> intellectual
> > value whatsoever" Alexander Stepanov
> > >
> >
> >
> >
> > --
> > "it might be a profitable thing to learn Java, but it has no intellectual
> > value whatsoever" Alexander Stepanov
> >
> > _______________________________________________
> >  Tutor maillist  -  Tutor at python.org
> >  http://mail.python.org/mailman/listinfo/tutor
> >
> >
>



-- 
"it might be a profitable thing to learn Java, but it has no intellectual
value whatsoever" Alexander Stepanov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081221/f73ffc9c/attachment-0001.htm>


More information about the Tutor mailing list