Word counting from file

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 5
Reputation: kenmeck03 is an unknown quantity at this point 
Solved Threads: 0
kenmeck03 kenmeck03 is offline Offline
Newbie Poster

Word counting from file

 
0
  #1
Oct 12th, 2009
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.

  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()
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 240
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 58
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #2
Oct 12th, 2009
try something like this for the file input:

  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.....
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: kenmeck03 is an unknown quantity at this point 
Solved Threads: 0
kenmeck03 kenmeck03 is offline Offline
Newbie Poster
 
0
  #3
Oct 12th, 2009
Thanks a bunch for that. It all makes sense now.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #4
Oct 12th, 2009
Originally Posted by kenmeck03 View 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.

  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:
  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.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC