| | |
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
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wikipedia windows write wxpython xlib






