实现统计文本中的字符、字母等
前言
F.txt
文本内容:
dasadahksadhkasHHKJJH K¥……¥……32423
代码实现
f = open("C:\\Users\\Desktop\\python\\F.txt", 'r', encoding="utf-8")
lines = f.read()
num_A = num_a = num_1 = num_str = num = 0
for i in lines:
if 'A' <= i <= 'Z':
num_A += 1
elif 'a' <= i <= 'z':
num_a += 1
elif "0" <= i <= "9":
num_1 += 1
elif i == " ":
num += 1
else:
num_str+=1
print("大写字母有{}个,小写字母有{}个,数字字符{}个,空格有{}个,其他字符有{}个".format(num_A,num_a,num_1,num,num_str))