[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

find string in file

fminervino

3/14/2008 1:25:00 PM

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
5 Answers

kaer

3/14/2008 1:55:00 PM

0

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')]
$

Tim Chase

3/14/2008 1:58:00 PM

0

> 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



Roman Dodin

3/14/2008 2:03:00 PM

0



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

mm007.emko

3/14/2008 4:37:00 PM

0

An of course, you can use a regular expression. (Module "re").

Lie Ryan

3/16/2008 9:13:00 AM

0

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.