| | |
find string in file
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
The simplest way, but maybe not the most efficient for large files, is this:
Note that find() only finds the first occurrence of the search word!
python Syntax (Toggle Plain Text)
# 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 """
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
•
•
Join Date: Dec 2006
Posts: 1,071
Reputation:
Solved Threads: 299
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)
found=the_string.find(word) while found > -1: print word, "found at location", found found=the_string.find(word, found+1)
Last edited by woooee; Mar 14th, 2008 at 7:33 pm.
0
#5 Nov 7th, 2009
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.
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.
May 'the Google' be with you!
•
•
Join Date: Nov 2009
Posts: 18
Reputation:
Solved Threads: 1
0
#6 Nov 7th, 2009
•
•
•
•
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.
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!!!
![]() |
Similar Threads
- Replacing text within a file using awk (Shell Scripting)
- Find word in file!! (C++)
- CSV file (C)
- find strings in file.txt (C++)
- Searching a file for a string (C++)
- String to disk with fstream (C++)
- need to find occurances in a string (C++)
Other Threads in the Python Forum
- Previous Thread: several questions about Glade & pygtk
- Next Thread: sudoku solver problem
Views: 7066 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy corners curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o iframe images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive screensaverloopinactive scrolledtext server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






