Help for a novice python coder

Thread Solved

Join Date: Sep 2008
Posts: 6
Reputation: pocnib is an unknown quantity at this point 
Solved Threads: 0
pocnib pocnib is offline Offline
Newbie Poster

Help for a novice python coder

 
0
  #1
Sep 2nd, 2008
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:

  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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Help for a novice python coder

 
0
  #2
Sep 2nd, 2008
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: pocnib is an unknown quantity at this point 
Solved Threads: 0
pocnib pocnib is offline Offline
Newbie Poster

Re: Help for a novice python coder

 
0
  #3
Sep 2nd, 2008
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....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC