I have a text file named textf that looks something like the following:
rxgmgcwbd c qcyurr bkxgmq, lwrg grru rrwxtam rwgzwt am quyam cv avrrgdwkxgcr.iwxbdamcz xdalguj qarc ram av vcmfwgmgum. yw'g
I want to do a frequency count for each letter in the text file but I want it with the condition that if a letter does not appear in the text, it should have a key:value pair with value 0. For example if z was not in the text it should look something like 'z': 0 and so on for all letters (a to z). I did the following code:
import string
from collections import Counter
with open("textf.txt") as tf:
letter = tf.read()
letter_count = Counter(letter.translate(str.maketrans('','',string.punctuation)))
print("Frequency count of letter:","\n",letter_count)
But the output looks something like this:
Counter({' ': 110, 'r': 12, 'c': 88, 'a': 55, 'g': 57, 'w': 76, 'm': 76, 'x': 72, 'u': 70, 'q': 41, 'y': 40, 'j': 36, 'l': 32, 'b': 18, 'd': 28, 'v': 27, 'k': 22, 't': 19, 'f': 18, 'z': 16, 'i': 7})
I am trying to make it so that the space count ' ': 110 is not shown and that I have all the letters(a-z) and when the letter does not appear in the text that my result prints something like 'n': 0 and so on. Any ideas or suggestions of how I could make this possible?