Here is a re-worked example of your script. What you want to do is get rid
of the global references and make your two functions take exactly the
arguments they need to do their work. And they should return whatever final
product they create.

I have made countWords simply take a file-like object to read from, and
returns your defaultdict to contain the count of each word.
Then I used writeWords to take that dict, and a destination file-like
object, and write out the formated results.

http://pastebin.com/MVH9LgNg

from collections import defaultdict

def countWords(sourceFile):
    wordCounter = defaultdict(int)
    for line in sourceFile:
        for word in line.strip().split():
            wordCounter[word.lower()] += 1

    return wordCounter


def writeWords(words, destFile):
    keys = sorted(words)
    largest = max(len(key) for key in keys)

    for word in keys:
        val = words[word]
        destFile.write('%s\t%d\n' % (word.rjust(largest), val))


if __name__ == "__main__":
    with open('in.txt') as in_file:
        wordsDict = countWords(in_file)

    with open("out.txt", "w") as out_file:
        writeWords(wordsDict, out_file)



On Fri, Nov 2, 2012 at 9:29 AM, Justin Israel <[email protected]>wrote:

> Ah I think I just got it...its probably this line?
>
> target.write('%s' % (print_words))
>
> print_words is a function you defined. You are trying to string format a
> function object. It is trying to treat it like an iterable, to unpack it
> into your %s placeholder. This theoretically would work if you were to call
> it like this:
>
> target.write('%s' % (print_words(someWords)))
>
> But the problem is that your print_words function doesn't return anything
> anyways.
>
>
> On Fri, Nov 2, 2012 at 9:27 AM, Justin Israel <[email protected]>wrote:
>
>> I am not getting the same results as you are because there are more
>> problems in this code that happen before I can even see that error. You
>> have a mixture of global references that don't seem to work properly.
>>
>> Can you include a complete traceback to identify the line that is failing?
>>
>> wordCounter is a defaultdict, which is an iterable, so I am not sure
>> which part is causing that error you mention.
>>
>>
>> On Fri, Nov 2, 2012 at 9:22 AM, damon shelton <[email protected]>wrote:
>>
>>> word counter is a python function object, and probably does not allow
>>> +=1 on it or calling wordCounter[word]
>>> maybe just make a standard int variable called count and +=1 on it
>>> everytime you find a word
>>>
>>> if you just send
>>> for line in source:
>>>     for word in line.split():
>>>         count += 1
>>>
>>>
>>> On Thu, Nov 1, 2012 at 9:15 PM, KI DONG PARK <[email protected]>wrote:
>>>
>>>> I got error when I try executing it that is 'function' object is not
>>>> iterable.
>>>> And I have no idea how I should improve it now.
>>>> Anybody give me some helping?
>>>> Thanks.
>>>>
>>>>  --
>>>> view archives: http://groups.google.com/group/python_inside_maya
>>>> change your subscription settings:
>>>> http://groups.google.com/group/python_inside_maya/subscribe
>>>>
>>>
>>>  --
>>> view archives: http://groups.google.com/group/python_inside_maya
>>> change your subscription settings:
>>> http://groups.google.com/group/python_inside_maya/subscribe
>>>
>>
>>
>

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to