Skip to main content

count_words_lines.py


"""
Challenge: count words and lines
aim: read a file provided and count the number of words and lines
Here are a few solutions, there are many other possible solutions
Author: L J Gregoire
"""
textfile = 'TheTyger_WilliamBlake.txt'
# solution 1: counting within loops
# loop over lines.
# add +1 to count
# split the line in words and add to word count
line_count = 0  # initialise the counters to 0
word_count = 0
f = open(textfile, 'r')
# skip the header lines
f.readline()
f.readline()
for line in f:
    if line.strip():  # if line not blank
        line_count+=1            # add to number of lines
        nwords=len(line.split()) # number of words on that line
        word_count+=nwords       # add to total number of words
print word_count,'words',line_count,'lines'
f.close()
# Solution 3: more python-style using lists of strings and counting their lenght
# here we count words per line and add them together
f = open(textfile, 'r')
# skip the header lines
f.readline()
f.readline()
all_lines=f.readlines()
filtered_lines=[ line for line in all_lines if line.strip()] # filter out blank lines
line_count=len(filtered_lines)  # get line count
words_per_line=[len(line.split()) for line in filtered_lines]  # list of word nb per line
word_count=sum(words_per_line)  # add all words together
print word_count,'words',line_count,'lines'