How to count text file character frequencies in Python

1 Answer

0 votes
f = open("d:\data.txt", "r")

character_frequencies = {}

for line in f.readlines():
    for ch in line.strip():
        character_frequencies[ch] = character_frequencies.get(ch, 0) + 1

for item in character_frequencies.items():
    print(item[0], " : ", item[1])


'''
run:
 
N  :  1
T  :  1
a  :  2
.  :  1
P  :  3
y  :  1
h  :  1
o  :  1
#  :  1
H  :  1
t  :  1
E  :  1
n  :  1
v  :  1
V  :  1
J  :  1
+  :  2
B  :  1
C  :  2

'''

 



answered Nov 15, 2018 by avibootz

Related questions

1 answer 162 views
1 answer 125 views
1 answer 198 views
1 answer 189 views
1 answer 214 views
...