Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread Alan Gauld
"Matt Herzog" wrote I can't help wondering how to do this in python: perl -wnl -e '/string/ and print;' filename(s) The first thing to say is that python is not Perl so there will be things Perl does more easily than Python and vice versa. (For example Pythons intersactive mode is miles be

Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread ppaarrkk
file1.read() works. What do I do if I want line numbers ? I found this code : http://snippets.dzone.com/posts/show/1638 src = open('2.htm').read() pattern = '([^<]+)' # or anything else for m in re.finditer(pattern, src): start = m.start() lineno = src.count('\n', 0, start) +

Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread Matt Herzog
- Forwarded message from Tiago Katcipis - i forgot, this might help you http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files I can't help wondering how to do this in python: perl -wnl -e '/string/ and print;' filename(s) Not that I want to forget the pre

Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread Tiago Katcipis
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,

Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread Tiago Katcipis
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 wrote: > i believe that the following should work > > file1 = open(fileO, 'r') > re.findall ('some_text', file1.read()) > > readlines returns

Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread Tiago Katcipis
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. b

[Tutor] Equivalent of grep in python

2008-12-21 Thread ppaarrkk
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/Equival