Jorgan Jorgan - 1 year ago 67
Python Question

How to get Counter to print frequency of unique words from each line of the input text file to the corresponding line on the output text file?

I have a homework question. I am supposed to write a function called "WordsByLine", which is supposed to count the frequency of unique words per line in the file and print the frequency of unique words to the corresponding line in an output file. My professor told us what the output is supposed to look like. For example, if the input text file says:

one fish two fish
red fish blue fish

(one fish two fish is the first line. red fish blue fish is the second line.)

The output has to look like this:

two:1 one:1 fish:2
red:1 blue:1 fish:2

The first line in the output is the frequency of unique words for the first line and then same for the second line.

Here is my code:

def wordsByLine(inFile, outFile):
from collections import Counter
outFile = open(outFile, 'w')
with open(inFile, 'r') as f:
freqs = Counter(f.readline().split())

But here is my output in the text file. It only prints out the first line.:

Counter({'two':1, 'one':1, 'fish':2})

How do I get the Counter to skip a line and print the frequency of unique words for the next line (from the next line in the input file)?

Answer Source

As pointed out in the comments, you only ever read a single line with f.readline. Also, you likely want to format your text instead of printing out the string representation of a Counter object:

>>> from collections import Counter
>>> def words_by_line(infile, outfile):
...     with open(infile) as f1, open(outfile, 'w') as f2: 
...         for line in f1:
...             counts = Counter(line.split())
...             string_gen = ("{}:{}".format(k,v) for k,v in counts.items())
...             f2.write(" ".join(string_gen) + "\n")
>>> words_by_line('input.txt','output.txt')


(trusty)juan@localhost:~$ cat output.txt 
fish:2 two:1 one:1
fish:2 red:1 blue:1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download