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 = '<P>([^<]+)<SUP>'  # or anything else
for m in re.finditer(pattern, src):
       start = m.start()
        lineno = src.count('\n', 0, start) + 1
        offset = start - src.rfind('\n', 0, start)
        word = m.group(1)
        print "2.htm(%s,%s): %s" % (lineno, offset, word)




Is there not a simpler way than this ?




ppaarrkk 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-tp21111356p21118578.html
Sent from the Python - tutor mailing list archive at Nabble.com.

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to