Wordcount exercise

536 views
Skip to first unread message

Bleys Andromeda-Focht

unread,
Jul 24, 2014, 10:46:34 AM7/24/14
to python-g...@googlegroups.com
Hi all!
 
During the course of my wordcount exercise, I was left with a couple questions. My first question is which file am I supposed to be converting, and how exactly do I call this file to read it. I have tried every different way of calling the file that I can think of as well.
 
I attempted to run the solution, and that didn't produce any results for me either. I think I am missing something here. I have included my code below.
 
Thanks in advance for all of your help!
 
Bleys
 
 
 
import sys
# +++your code here+++
def print_words(filename):
 file=open(r'C;\users\bfocht\bleys\python\google\basic\alice.txt', 'r+')
 wordcount={}
 #word=str.split()
 for word in file.read().split():
   if word not in wordcount:
     wordcount[word] = 1
   else:
     wordcount[word]+=1
 return file
sys.exit(0)
 
 
I have only worked on the above function.
 
 
 
 
# Define print_words(filename) and print_top(filename) functions.
# You could write a helper utility function that reads a file
# and builds and returns a word/count dict for it.
# Then print_words() and print_top() can just call the utility function.
###
# This basic command line argument parsing code is provided and
# calls the print_words() and print_top() functions which you must define.
def main():
  if len(sys.argv) != 3:
    print 'usage: ./wordcount.py {--count | --topcount} file'
    sys.exit(1)
  option = sys.argv[1]
  filename = sys.argv[2]
  if option == '--count':
    print_words(filename)
  elif option == '--topcount':
    print_top(filename)
  else:
    print 'unknown option: ' + option
    sys.exit(1)
if __name__ == '__main__':
  main()

Samuel Focht

unread,
Aug 4, 2014, 10:19:24 PM8/4/14
to python-g...@googlegroups.com
I am new to python as well. I saw the last name and thought that was awesome and had to post.  Have you tried python on code academy?  I have learned a lot.
Here is what I have so far.  It builds a dictionary.  I still need to write a function to sort the value.  If you run it  and then call d you will see the dictionary.

Hope this helped,
Sam



f = open(fileman, 'r')
lines = f.readlines()
f.close()
###
d = {}
for line in lines:
  words = line.split()
  for word in words:
    word = word.lower()
    if word not in d:
      d[str(word)] = 1
    else:
      d[str(word)] += 1
Message has been deleted
Message has been deleted

Katie McMillin

unread,
Aug 27, 2014, 4:19:55 AM8/27/14
to python-g...@googlegroups.com
Hi Bleys,

I think what you're asking is how to open the filename in order to call the function properly. Is that correct?

I had this issue and it's actually much simpler than I originally thought. The exercise is made to run the .txt files, small.txt and alice.txt included in the zip file. To open one of them, use them as the argument when calling the function. So either: print_words('small.txt') or print_words('alice.txt'). Don't use the actual .txt filename in the body of the function; use filename in that case.

I believe this can work with any .txt file that is in the same directory as the .py file.

import sys
# +++your code here+++
def print_words(filename):
 file=open(filename, 'r')

 wordcount={}
 #word=str.split()
 for word in file.read().split():
   if word not in wordcount:
     wordcount[word] = 1
   else:
     wordcount[word]+=1
 return file
sys.exit(0)
print_words('alice.txt') ##calls function to show that it works, but not part of function

I just bolded the sections that open the .txt file when the code is working correctly.

Hope this helps,

Katie
Reply all
Reply to author
Forward
0 new messages