Search code examples
pythonfrequencywordsword-frequency

Words, sorted by frequency, in a book (.txt file)


I'm using:

from collections import Counter
wordlist = open('mybook.txt','r').read().split()
c = Counter(wordlist)
print c

# result :
# Counter({'the': 9530, 'to': 5004, 'a': 4203, 'and': 4202, 'was': 4197, 'of': 3912, 'I': 2852, 'that': 2574, ... })

to print all the words of a book, sorted by frequency.

How to write this result in a .txt output file ?

g = open('wordfreq.txt','w')
g.write(c)   # here it fails 

Here is the desired output wordfreq.txt :

the, 9530
to, 5004
a, 5004
and, 4203
was, 4197
...


Solution

  • if you want to write it in a sorted manner you can do this.

    from collections import Counter
    wordlist = open('so.py', 'r').read().split()
    word_counts = Counter(wordlist)
    
    write_file = open('wordfreq.txt', 'w')
    for w, c in sorted(word_counts.iteritems(), key=lambda x: x[1], reverse=True):
        write_file.write('{w}, {c}\n'.format(w=w, c=c))