944,051 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 5169
  • Python RSS
Feb 28th, 2006
0

Is There Another Way To Write This Word Count Program Other Than This Way???

Expand Post »

Python Syntax (Toggle Plain Text)
  1. #The number of lines, and the number of words.
  2.  
  3.  
  4. import string
  5.  
  6.  
  7. def main():
  8. data = raw_input("Enter the path and name of your ")
  9. infile = file(data, 'r')
  10. data_file = infile.read()
  11. number_of_characters = len(data_file)
  12. print "The number of characters in your text is", number_of_characters
  13.  
  14. list_of_words = string.split(data_file)
  15. number_of_words = len(list_of_words)
  16. print "The number of words in your text is", number_of_words
  17. infile.close()
  18.  
  19. secondfile = file(data, 'r')
  20. line_of_text = secondfile.readlines()
  21. print line_of_text
  22. number_of_lines = len(lines_of_text)
  23. print "The number of lines in your text is" , number_of_lines
  24.  
  25. infile.close()
  26.  
  27. main()
Last edited by vegaseat; Mar 2nd, 2006 at 2:41 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
butterflyTee is offline Offline
43 posts
since Feb 2006
Mar 2nd, 2006
0

Re: Is There Another Way To Write This Word Count Program Other Than This Way???

You are making progress on your own. Just some small corrections and it does work. From here you can improve the code.
Python Syntax (Toggle Plain Text)
  1. #The number of lines, and the number of words.
  2.  
  3. #import string # not needed
  4.  
  5. def main():
  6. data = raw_input("Enter the path and name of your text file: ")
  7.  
  8. infile = file(data, 'r')
  9. data_file = infile.read()
  10. infile.close()
  11.  
  12. number_of_characters = len(data_file)
  13. print "The number of characters in your text is", number_of_characters
  14.  
  15. list_of_words = data_file.split()
  16. number_of_words = len(list_of_words)
  17. print "The number of words in your text is", number_of_words
  18.  
  19. secondfile = file(data, 'r')
  20. line_of_text = secondfile.readlines()
  21. secondfile.close()
  22. print line_of_text
  23. number_of_lines = len(line_of_text)
  24. print "The number of lines in your text is" , number_of_lines
  25.  
  26.  
  27. main()
A somewhat more streamlined version with a properly formatted output ...
Python Syntax (Toggle Plain Text)
  1. # The number of lines, words and characters in a text file.
  2.  
  3. def main():
  4. filename = raw_input("Enter the path and name of your text file: ")
  5.  
  6. infile = file(filename, 'r')
  7. lines_of_text = infile.readlines()
  8. infile.close()
  9.  
  10. number_of_lines = len(lines_of_text)
  11.  
  12. # join list of lines to form one string
  13. str1 = ''.join(lines_of_text)
  14. number_of_characters = len(str1)
  15.  
  16. # split string into a list of words
  17. list_of_words = str1.split()
  18. number_of_words = len(list_of_words)
  19.  
  20. # show the result
  21. print "%d %d %d %s" % (number_of_lines, number_of_words, number_of_characters, filename)
  22.  
  23.  
  24. main()
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 2nd, 2006
0

Re: Is There Another Way To Write This Word Count Program Other Than This Way???

thank you, that was a big help
Reputation Points: 10
Solved Threads: 0
Light Poster
butterflyTee is offline Offline
43 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: PyAUI-in wxPython
Next Thread in Python Forum Timeline: Tkinter Text tags





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


Follow us on Twitter


© 2011 DaniWeb® LLC