find string in file

Thread Solved

Join Date: Mar 2008
Posts: 1
Reputation: parsifal67 is an unknown quantity at this point 
Solved Threads: 0
parsifal67 parsifal67 is offline Offline
Newbie Poster

find string in file

 
0
  #1
Mar 14th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 666
Reputation: ZZucker is on a distinguished road 
Solved Threads: 38
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Practically a Master Poster

Re: find string in file

 
1
  #2
Mar 14th, 2008
The simplest way, but maybe not the most efficient for large files, is this:
  1. # sample text for testing
  2. # could come from a text file read like this:
  3. """
  4. infile = open("my_text.txt","r")
  5. text = infile.read()
  6. infile.close()
  7. """
  8.  
  9. text = """\
  10. My name is Fred Flintstone and I am a famous TV
  11. star. I have as much authority as the Pope, I
  12. just don't have as many people who believe it.
  13. """
  14.  
  15. search = "Fred"
  16. index = text.find(search)
  17. print search, "found at index", index
  18.  
  19. """
  20. my ouput (index is zero based) -->
  21. Fred found at index 11
  22. """
Note that find() only finds the first occurrence of the search word!
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster

Re: find string in file

 
1
  #3
Mar 14th, 2008
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
  1. found=the_string.find(word)
  2. while found > -1:
  3. print word, "found at location", found
  4. found=the_string.find(word, found+1)
Last edited by woooee; Mar 14th, 2008 at 7:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 18
Reputation: mitsuevo is an unknown quantity at this point 
Solved Threads: 1
mitsuevo mitsuevo is offline Offline
Newbie Poster
 
-1
  #4
20 Days Ago
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #5
19 Days Ago
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.
Last edited by vegaseat; 19 Days Ago at 12:05 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 18
Reputation: mitsuevo is an unknown quantity at this point 
Solved Threads: 1
mitsuevo mitsuevo is offline Offline
Newbie Poster
 
0
  #6
19 Days Ago
Originally Posted by vegaseat View Post
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!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven
 
0
  #7
19 Days Ago
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.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 18
Reputation: mitsuevo is an unknown quantity at this point 
Solved Threads: 1
mitsuevo mitsuevo is offline Offline
Newbie Poster
 
0
  #8
19 Days Ago
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC