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: butterflyTee is an unknown quantity at this point 
Solved Threads: 0
butterflyTee butterflyTee is offline Offline
Light Poster

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

 
0
  #1
Feb 28th, 2006

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

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

 
0
  #2
Mar 2nd, 2006
You are making progress on your own. Just some small corrections and it does work. From here you can improve the code.
  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 ...
  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()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 43
Reputation: butterflyTee is an unknown quantity at this point 
Solved Threads: 0
butterflyTee butterflyTee is offline Offline
Light Poster

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

 
0
  #3
Mar 2nd, 2006
thank you, that was a big help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC