944,089 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 960
  • Python RSS
Oct 12th, 2009
0

Word counting from file

Expand Post »
I need to prompt a user to type in a text file name and then take that file and and count the amount of words in the file. I have this code which counts the words in a file(that may not be "perfectly" written). I cannot figure out how to take the prompt text file and and get those words counted. I add a first line of say text = raw_input("enter a text file name: ") but I cannot take the file name given and have the words counted.

Python Syntax (Toggle Plain Text)
  1. text = open("rainfall.txt", "r")
  2. for t in text:
  3. words = t.split()
  4. wordCount = len(words)
  5. print wordCount
  6.  
  7. text.close()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kenmeck03 is offline Offline
5 posts
since Oct 2009
Oct 12th, 2009
0
Re: Word counting from file
try something like this for the file input:

Python Syntax (Toggle Plain Text)
  1. fileName = raw_input( "Enter filename( include .txt ): " )
  2. text = open( fileName, "r" )
  3. wordCount = 0
  4.  
  5. for t in text.readlines():
  6. wordsInLine = t.split( " " )
  7. wordCount += len( wordsInLine )
  8.  
  9. print "Word count is " + wordCount
  10. text.close()

This is in case the text in the file is correctly formated, i.e 1 space btw words.....
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Oct 12th, 2009
0
Re: Word counting from file
Thanks a bunch for that. It all makes sense now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kenmeck03 is offline Offline
5 posts
since Oct 2009
Oct 12th, 2009
0
Re: Word counting from file
Click to Expand / Collapse  Quote originally posted by kenmeck03 ...
I need to prompt a user to type in a text file name and then take that file and and count the amount of words in the file. I have this code which counts the words in a file(that may not be "perfectly" written). I cannot figure out how to take the prompt text file and and get those words counted. I add a first line of say text = raw_input("enter a text file name: ") but I cannot take the file name given and have the words counted.

Python Syntax (Toggle Plain Text)
  1. text = open("rainfall.txt", "r")
  2. for t in text:
  3. words = t.split()
  4. wordCount = len(words)
  5. print wordCount
  6.  
  7. text.close()
You are close:
python Syntax (Toggle Plain Text)
  1. fname = raw_input("enter a text file name: ")
  2.  
  3. fread = open(fname, "r")
  4. # read the whole file into one string
  5. text = fread.read()
  6. fread.close()
  7.  
  8. # split the whole text string into words at "white-spaces"
  9. word_list = text.split()
  10. word_count = len(word_list)
  11. print word_count
Last edited by bumsfeld; Oct 12th, 2009 at 3:48 pm.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

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: Finding the first letter of each string in a list
Next Thread in Python Forum Timeline: BaseHTTPServer





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


Follow us on Twitter


© 2011 DaniWeb® LLC