| | |
Word counting from file
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
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)
text = open("rainfall.txt", "r") for t in text: words = t.split() wordCount = len(words) print wordCount text.close()
0
#2 Oct 12th, 2009
try something like this for the file input:
This is in case the text in the file is correctly formated, i.e 1 space btw words.....
Python Syntax (Toggle Plain Text)
fileName = raw_input( "Enter filename( include .txt ): " ) text = open( fileName, "r" ) wordCount = 0 for t in text.readlines(): wordsInLine = t.split( " " ) wordCount += len( wordsInLine ) print "Word count is " + wordCount text.close()
This is in case the text in the file is correctly formated, i.e 1 space btw words.....
0
#4 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.
Python Syntax (Toggle Plain Text)
text = open("rainfall.txt", "r") for t in text: words = t.split() wordCount = len(words) print wordCount text.close()
python Syntax (Toggle Plain Text)
fname = raw_input("enter a text file name: ") fread = open(fname, "r") # read the whole file into one string text = fread.read() fread.close() # split the whole text string into words at "white-spaces" word_list = text.split() word_count = len(word_list) print word_count
Last edited by bumsfeld; Oct 12th, 2009 at 3:48 pm.
Should you find Irony, you can keep her!
![]() |
Similar Threads
- Text Editor using MS Word .doc file (VB.NET)
- How we open a ms word document file with php? (PHP)
- Counting Occurences of a Word in a Text File (C#)
- c++ find word from text file (C++)
- opening a excel from word macro by getting the file name from word at runtime (Visual Basic 4 / 5 / 6)
- Need help!!!search word from text file and display lines which match the word. (C++)
Other Threads in the Python Forum
- Previous Thread: Finding the first letter of each string in a list
- Next Thread: BaseHTTPServer
Views: 410 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt apache approximation argv array beginner binary book calculator change cipher clear code converter countpasswordentry cturtle dictionaries dictionary drive dynamic examples excel file float format ftp function gui hints homework import inches input java keyboard library line linux list lists loop maze mouse mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive remote script scrolledtext search session signal singleton socket ssh string strings strip table terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable verify vigenere windows wordgame wxpython xlwt






