[Tutor] Lance E Sloan"help"
python
python <python@inkedmn.net>
Wed, 21 Aug 2002 10:26:16 -0700
hello,
i added some stuff to that code, here it is:
def getStuff(file):
info = open(file, 'r')
infolines = info.readlines()
linenum = 1
totalwords = 0
for line in infolines:
words = line.split(' ')
print "Line %d contains %d words" % (linenum, len(words))
linenum += 1
totalwords += len(words)
print "Total lines:", linenum - 1
print "Average words per line:", float(totalwords)/linenum - 1
file = raw_input("Enter the filename: ")
getStuff(file)
and here's what the output looks like:
C:\python wordcount.py
Enter the filename: gpl.txt
...
...
Line 339 contains 15 words
Line 340 contains 6 words
Line 341 contains 1 words
Total lines: 341
Average words per line: 8.88011695906
is that more like what you want?
brett
M> Lance E Sloan
M> Hi
M> No this is not a class im taking but my boss , he is a Delphi and Unix and python expert and he wants me to do this project
M> I have tried some code but cant seem to get started and by friday it must be done.
M> here is what i have
M> # line/word counter
M> def getStuff(file):
M> info = open(file, 'r')
M> infolines = info.readlines()
M> linenum = 1
M> for line in infolines:
M> words = line.split(' ')
M> print "Line %d contains %d words" % (linenum, len(words))
M> linenum += 1
M> file = raw_input("Enter the filename: ")
M> getStuff(file)
M> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
M> here is what I need to do
M> c I need to reate a Python program that takes a text file and counts the number of
M> lines in the file, the number of words in a line and then produces a
M> summary. The Output must look like this.
M> Enter file name:
>> testfile.txt
M> LINE# WORDS
M> --------------------
M> 1 20 words
M> 2 25 words
M> 3 30 words
M> 4 25 words
M> --------------------
M> Summary:
M> 4 lines, 100 words, Avg: 25 words/line
M> Please let me know soon
M> Morne