CSC111 countwords.py

From DftWiki

Jump to: navigation, search

--D. Thiebaut 13:17, 21 April 2010 (UTC)


# countwords.py
# D. Thiebaut
# A program computing some statistics about text input
# at the keyboard.

import sys
import string

def main():
    filename = raw_input( "text file? " )

    wordFreq = {}
    file = open( filename, 'r' )
    lines = file.readlines()
    file.close()


    for line in lines:
        #--- remove punctuation ---
        for c in string.punctuation:
            line = line.replace( c, ' ' )

        #--- split into words ---
        line = line.lower()
        words = line.split( )
        for word in words:
            print "-", word


main()