sammie
sammie

Reputation: 21

python, how to count most common words in text file

I am very new to Python but I need to create a program which will count the most common words in two text files and then print how often these words appear , ordered by most common first. I have the following code so far, as you can see I am very stuck and lost!!

import re
import collections

with open('Bible txt.txt') as f:
text = f.read()

words = re.compile(r"a-zA-Z'").findall(text)
counts = collections.Counter(words)

there are no errors but when I run it comes up with "Process finished with error code = 0"

I know there are other questions like this but none of the other methods I have tried seem to work. I am using PyCharm CE for this.

Any help would be appreciated

Upvotes: 0

Views: 5280

Answers (1)

Greg Eremeev
Greg Eremeev

Reputation: 1840

from collections import Counter

with open('Bible txt.txt') as fin:
    counter = Counter(fin.read().strip().split())

print(counter.most_common())

Upvotes: 1

Related Questions