| | |
RE: word count programming for python (wc)
![]() |
Assuming the question does not want a count of unique words/characters this should do it:
Obviously this is not python but pseudo code that will very easily translate into python - I'll leave that to you. Look into string functions for splitting strings into a list of words (hint). The length of this list will give you the number of words. The length of a string is the number of characters and the number of line reads is the number of lines.
Python Syntax (Toggle Plain Text)
open the file for reading number of words,characters, lines = 0 while a line can be read from the file read a line into a string number of lines + = 1 number of characters + = length of string number of words + = length of string split into a list of words print out number of words, lines, characters
Obviously this is not python but pseudo code that will very easily translate into python - I'll leave that to you. Look into string functions for splitting strings into a list of words (hint). The length of this list will give you the number of words. The length of a string is the number of characters and the number of line reads is the number of lines.
Sorry for not getting back sooner but it seems other kind folks in the forum have answered already.
It's hard to gauge someones level of experience when answering a question but I only outlined the program's logic broadly.
I recommend studying a beginners tutorial on python and playing around in the interpreter, following examples etc to get a feel for things.
Personally, if you are just starting out with programming in general, I wouldn't recommend developing gui front ends just yet. Perhaps just "raw_input() to read input from the user and "print " to output it.
Please don't take offence if I underestimate your level of experience and don't think I'm being deliberately vague in offering a solution. If I were to simply give you python code for a complete working program I don't think you'd learn as much as doing it for yourself. If this is a homework assignment, simply copying an offered solution verbatim will give a false impression of your proficiency.
Good luck with python !
It's hard to gauge someones level of experience when answering a question but I only outlined the program's logic broadly.
I recommend studying a beginners tutorial on python and playing around in the interpreter, following examples etc to get a feel for things.
Personally, if you are just starting out with programming in general, I wouldn't recommend developing gui front ends just yet. Perhaps just "raw_input() to read input from the user and "print " to output it.
Please don't take offence if I underestimate your level of experience and don't think I'm being deliberately vague in offering a solution. If I were to simply give you python code for a complete working program I don't think you'd learn as much as doing it for yourself. If this is a homework assignment, simply copying an offered solution verbatim will give a false impression of your proficiency.
Good luck with python !
I asked this question to someone back when I was coding my text editor(horrible project). THis is what that person gave me to use.
ITs a function that counts words. Hope this helps!
Python Syntax (Toggle Plain Text)
def count_words(self): text = self.text.get(1.0, 'end') words = len(text.split()) print words self.master.title("Text has %d words" % words)
![]() |
Similar Threads
- Why are there so many languages? (Computer Science)
Other Threads in the Python Forum
- Previous Thread: import file search
- Next Thread: pyGTK and the Mandate of Sudo
| Thread Tools | Search this Thread |
abrupt accessdenied advanced ansi anti apache application approximation argv array backend beginner binary builtin calculator change command converter countpasswordentry csv curved dan08 def dictionary edit event file float format function google heads homework inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric obexftp output parameters parsing path phonebook pointer prime programming py2exe pygame pyopengl python random recursion redirect remote return reverse scrolledtext session software sprite statictext statistics string strings syntax terminal text thread threading time tlapse tuple twoup ubuntu unicode unit urllib urllib2 variable voip wordgame write wxpython





