944,030 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 36188
  • Python RSS
Mar 14th, 2008
0

find string in file

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
parsifal67 is offline Offline
1 posts
since Mar 2008
Mar 14th, 2008
1

Re: find string in file

The simplest way, but maybe not the most efficient for large files, is this:
python Syntax (Toggle Plain Text)
  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!
Reputation Points: 309
Solved Threads: 43
Practically a Master Poster
ZZucker is offline Offline
676 posts
since Jan 2008
Mar 14th, 2008
1

Re: find string in file

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
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Nov 7th, 2009
-1
Re: find string in file
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!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mitsuevo is offline Offline
21 posts
since Nov 2009
Nov 7th, 2009
0
Re: find string in file
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; Nov 7th, 2009 at 12:05 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 7th, 2009
0
Re: find string in file
Click to Expand / Collapse  Quote originally posted by vegaseat ...
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!!!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mitsuevo is offline Offline
21 posts
since Nov 2009
Nov 7th, 2009
0
Re: find string in file
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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Nov 7th, 2009
0
Re: find string in file
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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mitsuevo is offline Offline
21 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: several questions about Glade & pygtk
Next Thread in Python Forum Timeline: sudoku solver problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC