943,847 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 15723
  • Python RSS
Feb 19th, 2007
0

Find a word in a file

Expand Post »
I want to see if a certain word is in a text file, how do I go about that?
Similar Threads
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Feb 19th, 2007
0

Re: Find a word in a file

Click to Expand / Collapse  Quote originally posted by sneekula ...
I want to see if a certain word is in a text file, how do I go about that?
you can read in line by line
Python Syntax (Toggle Plain Text)
  1. for line in open("file"):
  2. if "word" in line:
  3. do_something(line)

you can read in one whole chunk
Python Syntax (Toggle Plain Text)
  1. data = open("file").read()
  2. if "someword" in data:
  3. do_something()
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Feb 20th, 2007
0

Re: Find a word in a file

Holy smokes, ghostdog, you make it look real simple! Looks like Python really has the power of simplicity and elegance! Besides, I can actually understand the code as I read it!
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Apr 11th, 2007
0

Re: Find a word in a file

for better maching of word , go through re module of python, you must find some interesting thing there..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sharma_vivek82 is offline Offline
18 posts
since Mar 2006

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: Python tuples and Sql query
Next Thread in Python Forum Timeline: Problem with threads





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


Follow us on Twitter


© 2011 DaniWeb® LLC