HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Count the frequency of words in a text file

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thefilefrequencywordstextcount

Problem

#!/usr/bin/python
file=open("C:/python27/python operators.txt","r+")

wordcount={}

for word in file.read().split():
    if word not in wordcount:
        wordcount[word] = 1
    else:
        wordcount[word] += 1

for k,v in wordcount.items():
    print k, v

Solution

Use a Context Manager

When dealing with something like a file, it's safer to write:

with open(filename) as file:
    ## code ##


That will automatically handle file closing correctly in case of exceptions and the like. I also find it clearer. Also, why r+?

Prefer Generators

file.read() reads the entire file into memory. Prefer to just go a line at a time by iterating through it.

wordcount.items() gives you a full list of all the items. But you don't need them all at once, you just need to iterate through them. For that there's iteritems().

Use the tools at your disposal

You have:

if word not in wordcount:
    wordcount[word] = 1
else:
    wordcount[word] += 1


That's a lot of code for a simple operation. What are you doing here? You're counting the incidence of word. But there's an app for that: collections.Counter:

wordcount = collections.Counter()
for line in file:
    for word in line.split():
        wordcount[word] += 1


Furthermore, we also have Counter.update, which lets us do:

for line in file:
    wordcount.update(line.split())


Full solution:

#!/usr/bin/python
import collections

wordcount = collections.Counter()
with open("C:/python27/python operators.txt") as file:
    for line in file:
        wordcount.update(line.split())

for k,v in wordcount.iteritems():
    print k, v

Code Snippets

with open(filename) as file:
    ## code ##
if word not in wordcount:
    wordcount[word] = 1
else:
    wordcount[word] += 1
wordcount = collections.Counter()
for line in file:
    for word in line.split():
        wordcount[word] += 1
for line in file:
    wordcount.update(line.split())
#!/usr/bin/python
import collections

wordcount = collections.Counter()
with open("C:/python27/python operators.txt") as file:
    for line in file:
        wordcount.update(line.split())

for k,v in wordcount.iteritems():
    print k, v

Context

StackExchange Code Review Q#103773, answer score: 12

Revisions (0)

No revisions yet.