Skip to main content

count_words_lines_part2.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'
newtextfile = 'count_TheTyger_WilliamBlake.txt'
newtextfile2 = 'count2_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')
fout=open(newtextfile, 'w')
# deal with the header first
header1=f.readline()
fout.write(header1)
header2=f.readline()
fout.write(header2)
# now the main text
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
        fout.write(str(nwords)+' '+line)
    else:
        fout.write('\n')
print word_count,'words',line_count,'lines'
f.close()
fout.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')
fout2=open(newtextfile2, 'w')
# skip the header lines
header1=f.readline()
fout2.write(header1)
header2=f.readline()
fout2.write(header2)
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'
for i in range(len(filtered_lines)):
    fout2.write(str(words_per_line[i])+' '+filtered_lines[i])
fout2.close()