| | |
Is There Another Way To Write This Word Count Program Other Than This Way???
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2006
Posts: 43
Reputation:
Solved Threads: 0
Python Syntax (Toggle Plain Text)
#The number of lines, and the number of words. import string def main(): data = raw_input("Enter the path and name of your ") infile = file(data, 'r') data_file = infile.read() number_of_characters = len(data_file) print "The number of characters in your text is", number_of_characters list_of_words = string.split(data_file) number_of_words = len(list_of_words) print "The number of words in your text is", number_of_words infile.close() secondfile = file(data, 'r') line_of_text = secondfile.readlines() print line_of_text number_of_lines = len(lines_of_text) print "The number of lines in your text is" , number_of_lines infile.close() main()
Last edited by vegaseat; Mar 2nd, 2006 at 2:41 am.
You are making progress on your own. Just some small corrections and it does work. From here you can improve the code.
A somewhat more streamlined version with a properly formatted output ...
Python Syntax (Toggle Plain Text)
#The number of lines, and the number of words. #import string # not needed def main(): data = raw_input("Enter the path and name of your text file: ") infile = file(data, 'r') data_file = infile.read() infile.close() number_of_characters = len(data_file) print "The number of characters in your text is", number_of_characters list_of_words = data_file.split() number_of_words = len(list_of_words) print "The number of words in your text is", number_of_words secondfile = file(data, 'r') line_of_text = secondfile.readlines() secondfile.close() print line_of_text number_of_lines = len(line_of_text) print "The number of lines in your text is" , number_of_lines main()
Python Syntax (Toggle Plain Text)
# The number of lines, words and characters in a text file. def main(): filename = raw_input("Enter the path and name of your text file: ") infile = file(filename, 'r') lines_of_text = infile.readlines() infile.close() number_of_lines = len(lines_of_text) # join list of lines to form one string str1 = ''.join(lines_of_text) number_of_characters = len(str1) # split string into a list of words list_of_words = str1.split() number_of_words = len(list_of_words) # show the result print "%d %d %d %s" % (number_of_lines, number_of_words, number_of_characters, filename) main()
May 'the Google' be with you!
![]() |
Similar Threads
- Word Count Issues (Python)
- word count (C++)
- Word count help. (C++)
- Can you please help me write this word count program in another way (Python)
- I Need Help Writing A Word Count Program In My Python (Python)
- word count in borland c++ ?? (C++)
- word count (Java)
- I can't implement a word count into my text editor (JAVA) (Java)
Other Threads in the Python Forum
- Previous Thread: PyAUI-in wxPython
- Next Thread: How to run IDLE in linux?
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti approximation assignment avogadro backend basic beginner binary bluetooth calculator character code customdialog decimals dictionaries dictionary drive dynamic examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects py2exe pygame pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






