Count letters in a text file

This is very readable way to accomplish what you want using Counter:

from string import ascii_lowercase
from collections import Counter

with open('text.txt') as f:
    print Counter(letter for line in f 
                  for letter in line.lower() 
                  if letter in ascii_lowercase)

You can iterate the resulting dict to print it in the format that you want.

Leave a Comment