Python Count every char in a text file

letters = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’,’k’,’l’,’m’,’n’,’o’,’p’,’q’,’r’,’s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’,’A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’,’M’,’N’,’O’,’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’]

counter = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

i = 0

print “Started”

with open(‘alice.txt’) as f:
while True:
c = f.read(1)
if not c:
print “Done”
break
for letter in letters:
if c == letter:
counter[i] = counter[i] + 1
i = 0
break
i = i + 1
if i == 52:
i = 0

i = 0

for letter in letters:
print i, letter, counter[i]
i = i + 1

print letters
print counter

Please follow and like us: