PDA

View Full Version : [SOLVED] [Python] Print matching lines in a file



durand
June 30th, 2008, 09:46 PM
Hi,
This should be a pretty simple question but I can't seem to find an answer on the web. How would you do the equivalent of:


grep file "string" with python. I've found one method so far using re.

Here it is:


pattern = re.compile( Variable1 )

for line in data:
a_match = pattern.search( line )
if ( a_match ):
print line


The problem is that "string", or Variable1 in the code, should not be parsed for regex.

Thanks in advance :)

Can+~
June 30th, 2008, 10:03 PM
You don't want to parse it as a regexp, then why are you using regexps in the first place?

Matching exactly the line (w/o regexp):

search_str = "hello"
search_file = open("myfile", "r")

for line in search_file:
if line.strip() == search_str:
print line

search_file.close()


Matching a substring

search_str = "hello"
search_file = open("myfile", "r")

for line in search_file:
if line.strip().find(search_str) != -1:
print line

search_file.close()


Check string methods:
http://docs.python.org/lib/string-methods.html

(Extra: Add try/except IOError/finally when managing files).

LaRoza
June 30th, 2008, 10:08 PM
for line in open("unix.text"):
if "Gurus" in line:
print line


~$cat unix.text
Get GUMMed
----------

The Gurus of Unix Meeting of Minds (GUMM) takes place Wednesday, April 1, 2076
(check THAT in your perpetual calendar program), 14 feet above the ground
directly in front of the Milpitas Gumps. Members will grep each other by the
hand (after intro), yacc a lot, smoke filtered chroots in pipes, chown with
forks, use the wc (unless uuclean), fseek nice zombie processes, strip, and
sleep, but not, we hope, od. Three days will be devoted to discussion of the
ramifications of whodo. Two seconds have been allotted for a complete rundown
of all the user-friendly features of Unix. Seminars include "Everything You
Know is Wrong", led by Tom Kempson, "Batman or Cat:man?" led by Richie Dennis
"cc C? Si! Si!" led by Kerwin Bernighan, and "Document Unix, Are You
Kidding?" led by Jan Yeats. No Reader Service No. is necessary because all
GUGUs (Gurus of Unix Group of Users) already know everything we could tell
them.
-- Dr. Dobb's Journal, June 1984

Can+~
June 30th, 2008, 10:13 PM
for line in open("unix.text"):
if "Gurus" in line:
print line


Right, I always forget the in operator. :KS

LaRoza
June 30th, 2008, 10:14 PM
Right, I always forget the in operator. :KS

It is handy :-)

It is also faster I think.

durand
July 1st, 2008, 03:34 PM
Thanks both of you! I used LaRoza's method because it was a lot simpler. I thought I tried that yesterday but my IDE froze for a while. I think I know why. It outputs a lot very quickly so redrawing everything uses up the cpu completely.

One more question, what is the preferred way of outputting to a text file and would it work on every platform? At the moment I'm using print:


print >> file, "string"
but I get IOError: [Errno 2] No such file or directory if the file doesn't exist. Is there the equivalent of unix touch in python? (has to be cross platform)

Thanks again.

geirha
July 1st, 2008, 04:39 PM
You need to open the file in write or append mode. help(file)


f= file("/tmp/foo.txt", "w")
print >> f, "something"
f.close()

durand
July 1st, 2008, 04:43 PM
Okie, I was using r+ instead of w, maybe that was my problem....Thanks.