943,620 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 566
  • Python RSS
Sep 2nd, 2008
0

Help for a novice python coder

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  1. import sys
  2.  
  3. word_dictionary = {}
  4. l = []
  5. m = []
  6.  
  7. for s in sys.stdin:
  8. l = s.split() #split the line into a list of words
  9. for i in l:
  10. m.append(i) #add elements of l to m since l will be overwritten
  11. for j in m:
  12. j.strip(' ,.?!')
  13. j.lower()
  14. m.sort()
  15. for k in m:
  16. if k in word_dictionary:
  17. word_dictionary[k]=word_dictionary[k]+1
  18. else:
  19. word_dictionary[k]=1
  20. for word in word_dictionary:
  21. 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pocnib is offline Offline
6 posts
since Sep 2008
Sep 2nd, 2008
0

Re: Help for a novice python coder

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....
Last edited by jlm699; Sep 2nd, 2008 at 11:40 pm.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Sep 2nd, 2008
0

Re: Help for a novice python coder

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....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pocnib is offline Offline
6 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Problem with name not defined
Next Thread in Python Forum Timeline: Reloading a Python Script?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC