| | |
find string in file
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: 999
Reputation:
Solved Threads: 283
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 16 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.
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; 16 Days Ago at 12:05 pm.
May 'the Google' be with you!
•
•
Join Date: Nov 2009
Posts: 18
Reputation:
Solved Threads: 1
0
#6 16 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.
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
| Thread Tools | Search this Thread |
accessdenied advanced apache application argv array beginner book builtin calculator change command converter countpasswordentry csv curved dan08 def dictionary dynamic edit enter event file float format function google homework import inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric obexftp output parameters parsing path phonebook plugin prime programming py2exe pygame pyopengl python random recursion redirect remote return reverse scrolledtext session simple skinning software sprite statictext string strings syntax table terminal text textarea threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable voip wordgame wxpython






