1,105,391 Community Members

find string in file

Member Avatar
parsifal67
Newbie Poster
1 post since Mar 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
ZZucker
Practically a Posting Shark
829 posts since Jan 2008
Reputation Points: 313 [?]
Q&As Helped to Solve: 89 [?]
Skill Endorsements: 2 [?]
 
1
 

The simplest way, but maybe not the most efficient for large files, is this:

# sample text for testing
# could come from a text file read like this:
"""
infile = open("my_text.txt","r")
text = infile.read()
infile.close()
"""

text = """\
My name is Fred Flintstone and I am a famous TV
star.  I have as much authority as the Pope, I
just don't have as many people who believe it.
"""

search = "Fred"
index = text.find(search)
print search, "found at index", index

"""
my ouput (index is zero based) -->
Fred found at index 11
"""

Note that find() only finds the first occurrence of the search word!

Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
1
 

FWIW this will find all occurrences of a word in a string. Also note that if you are looking for "the" it will give a hit for "then". If you only want the word, you have to search with spaces added, " the " -assuming string.lower() and that punctuation is removed

found=the_string.find(word)
while found > -1:
     print word, "found at location", found
     found=the_string.find(word, found+1)
Member Avatar
mitsuevo
Newbie Poster
21 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-1
 

I kinda have the sane problem, but i want to extract the text ocr several lines.. not just a word...
Can somone tell me how to use a RE for this?
Thanks!

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

You should really start a new thread of your own!

The string function find() can find a subtext within a larger text, as will re.findall(). The advantage of find() is that it will give you the location in the form of an index.

Question Answered as of 4 Years Ago by ZZucker, mitsuevo, vegaseat and 1 other
Member Avatar
mitsuevo
Newbie Poster
21 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

You should really start a new thread of your own!

The string function find() can find a subtext within a larger text, as will re.findall(). The advantage of find() is that it will give you the location in the form of an index.

Thanks, but i'm a bit unsure on using the index since the length of data tends to change within the search area...

And sorry about not starting a new thread... cos in another hobby forum that i'm part of we try to post questions in a thread thats already started with a similar question so that other readers can also look back on the old answer.. i guess it depends on the forum :)

Thanks again guys. i'm new to DaniWeb and am loving it here! you guys are amazingly helpful!!!

Member Avatar
sneekula
Nearly a Posting Maven
2,496 posts since Oct 2006
Reputation Points: 917 [?]
Q&As Helped to Solve: 263 [?]
Skill Endorsements: 5 [?]
 
0
 

Ah, the pros and cons on posting on an old thread.
If you are interested in a regex solution, I would start a new thread and put regex in the title.

Member Avatar
mitsuevo
Newbie Poster
21 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

A friend just gave me a basic (not very professinal, but easy to move around) solution... trying that out... if it doesnt work, i sure will throw the problem at you guys again :)

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article