954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I Need Help Writing A Word Count Program In My Python

AM I DOING THIS RIGHT, PLEASE HELP ME, SO ME MORE EXAMPLES ON HOW TO RIGHT IT :o

C:\home\COP1000\a\3>python wc.py futval.py test.txt wc.py
22 103 699 futval.py
2 4 22 test.txt
52 223 1683 wc.py
76 330 2404 total

butterflyTee
Light Poster
43 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

You got to show us the code for wc.py. Most of us are not very good in mind reading!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

So I Didnt Do This Right

butterflyTee
Light Poster
43 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

I got to know what you have been asked to do with your Python program, before I can tell you if you did it right!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Word Count. A Common Utility On Unix/linux Systems Is A Small Program Called "wc." This Program Analyzes A File To Determine The Number Of Lines, Words, And Characters Contained Therein. Write A Version Of Wc. The Program Should Accept A File Name As Input And Than Print Three Numbers Showing The Count Of Lines, Words, And Characters In The File.

butterflyTee
Light Poster
43 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

there is spaces between the numbers

butterflyTee
Light Poster
43 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

Here is simplified approach, the number of new-line characters '\n' give number of lines, the number of spaces are approximately the number of words, and the number of characters is the length of the text string read from file.

You simply use a for loop and and iterate each character in text counting new-lines, spaces. Don't have Unix and don't know if space counts as a character, also new-line. Can use char_from_text.isalnum() to weed these out.

You can also use, I didn't test, number_of_lines = text.count('\n') function.

A more correct answer for the number of words can be obtained by splitting the text string into list of words with word_list = text.split() and then get length of this list with number_words = len(word_list).

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

so how would i set this up with what u told me because im a visual learner
C:\home\COP1000\a\3>python wc.py futval.py test.txt wc.py
22 103 699 futval.py
2 4 22 test.txt
52 223 1683 wc.py
76 330 2404 total

butterflyTee
Light Poster
43 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

Here is simplified approach, the number of new-line characters '\n' give number of lines, the number of spaces are approximately the number of words, and the number of characters is the length of the text string read from file.

You simply use a for loop and and iterate each character in text counting new-lines, spaces. Don't have Unix and don't know if space counts as a character, also new-line. Can use char_from_text.isalnum() to weed these out.

You can also use, I didn't test, number_of_lines = text.count('\n') function.

A more correct answer for the number of words can be obtained by splitting the text string into list of words with word_list = text.split() and then get length of this list with number_words = len(word_list).
i having problem using this set up, can u please help me
C:\home\COP1000\a\3>python wc.py futval.py test.txt wc.py
22 103 699 futval.py
2 4 22 test.txt
52 223 1683 wc.py
76 330 2404 total

butterflyTee
Light Poster
43 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You