I know this is a homework question, but I have reached the end of my rope and really need guidance. I have had four years of Java programming then walked into my CS 3 class and was given an assignment in python. I have never worked in Python before but tried writing some sample programs that seemed to work OK, but this project just won't cooperate. We have a text file(the Gettysburg Address) and we have to read it from sys.stdin into a dictionary which is to be sorted and have each word only once as the key with the word count for the value, then print out the dictionary. What I have is:

import sys

word_dictionary = {}
l = []
m = []

for s in sys.stdin:
     l = s.split() #split the line into a list of words
     for i in l:
          m.append(i) #add elements of l to m since l will be overwritten
for j in m:
     j.strip(' ,.?!')
for k in m:
     if k in word_dictionary:
for word in word_dictionary:
     print word,word_dictionary[word]

However when I run this(python lab1.py getty.txt) nothing happens. If I put a print statement at the top where I initialize the lists and dictionary, it gets printed, but no other print statements are reached. Is there something wrong with my code? Or is my computer just really slow at reading the text file? Any help for an absolute novice?

8 Years
Discussion Span
Last Post by pocnib

You're never reading your file... you're literally trying to read from sys.stdin. This is wrong and I think you may have misunderstood your prof.

You need to open the file via open(file_name, mode) ... then read from there....


Thanks, I actually realized my problem, I was running the program
python lab1.py getty.txt
instead of
python lab1.py < getty.txt

It works like that....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.