preparation : Create a txt file , Put it in the directory where the source file is located . This example uses student.txt As a demonstration
Complete code :
# Declaration file name
fileName = "student.txt"
# Open as read-only txt file
ll = open(fileName,"r")
# Read all the data in the file
data = ll.read()
count = 0
res = {}
for ch in data:
# Judge whether the current character is a letter
if ch.isalpha():
if ch not in res:
res[ch] = 1
else :
res[ch] = res[ch] + 1
print(res)
The operation results are as follows :
Letter a appear 2 Time , Letter b appear 3 Time ......