By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

find string in file

P: n/a
Hi friends !!

I'm neophite about python, my target is to create a programa that
find a specific string in text file.
How can do it?

Thanks
fel
Mar 14 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 14 mar, 14:25, fminerv...@gmail.com wrote:
Hi friends !!

I'm neophite about python, my target is to create a programa that
find a specific string in text file.
How can do it?

Thanks
fel
$ cat text.txt
aaa
bbb
ccc
ddd
aaa bbb
ccc ddd
aaa bbb ccc ddd
aaa eee
bbb eee
ccc eee
ddd eee
$ cat bin/find_string.py
import sys
file_name=sys.argv[1]
searched_string=sys.argv[2]
result= [(line_number+1, line) for line_number, line in
enumerate(open(file_name)) if searched_string in line]
print result
$ python bin/find_string.py text.txt eee
[(8, 'aaa eee\n'), (9, 'bbb eee\n'), (10, 'ccc eee\n'), (11, 'ddd eee
\n')]
$ python bin/find_string.py text.txt aaa
[(1, 'aaa\n'), (5, 'aaa bbb\n'), (7, 'aaa bbb ccc ddd\n'), (8, 'aaa eee
\n')]
$ python bin/find_string.py text.txt ddd
[(4, 'ddd\n'), (6, 'ccc ddd\n'), (7, 'aaa bbb ccc ddd\n'), (11, 'ddd
eee\n')]
$
Mar 14 '08 #2

P: n/a
I'm neophite about python, my target is to create a programa that
find a specific string in text file.
How can do it?
>>FNAME1 = 'has.txt'
FNAME2 = 'doesnt_have.txt'
TEXT = 'thing to search for'
TEXT in file(FNAME1).read()
True
>>TEXT in file(FNAME2).read()
False

or that may not be what your teacher wants, but if you give the
homework problem and what you've tried, we might be able to give
you some more quality guidance. :)

The above is *really* *bad* code...horribly suboptimal especially
as files get large. It only returns a boolean value. You might
prefer to enumerate the file's contents and, if any line contains
the search-text, return that line offset without reading the rest
of the file.

-tkc

Mar 14 '08 #3

P: n/a


On 14 mar, 14:25, fminerv...@gmail.com wrote:
>Hi friends !!

I'm neophite about python, my target is to create a programa that
find a specific string in text file.
How can do it?

Thanks
fel


One more way to do this

f = codecs.open("log.txt", 'r', "utf_16_le")
lines = f.readlines()
for line in lines:
if(line.find("my string to find")!=-1):
print line
Mar 14 '08 #4

P: n/a
An of course, you can use a regular expression. (Module "re").
Mar 14 '08 #5

P: n/a
Lie
On Mar 14, 8:25*pm, fminerv...@gmail.com wrote:
Hi friends !!

I'm neophite about python, my target is to create a programa that
find *a specific string in text file.
How can do it?

Thanks
fel
I'd recommend regular expression (import re) if the string you're
searching is in a complex format (like all strings that starts with a
dollar sign followed by an arbitrary number of numbers which might or
might not contain commas every three number).

If what you're searching is just simple queries (like searching where
"Hello" appeared in the text you'd better use the string modules. as
they're simpler to use and faster.
Mar 16 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.