编写程序实现对特定英文文章(文本文件)的单词数和有效行数的统计,其中要求空行不计数
# 统计一行字符的不同字符个数
str = input("请输入一行字符:")
count1 = count2 = count3 = 0
for s in str:
if 'a' <= s <= 'z' or 'A' <= s <= 'Z':
count1 += 1 # 英文计数
elif 0x4e00 <= ord(s) <= 0x9fa5: # 中文的Unicode编码范围
count2 += 1 # 中文计数
elif 48 <= ord(s) and ord(s) <= 57:
count3 += 1 # 数字计数
print("该字符串有空格{0}个".format(str.count(" "))) # 统计空格
print("该字符串有英文字符{0}个".format(count1)) # 统计英文字符
print("该字符串有中文字符{0}个".format(count2)) # 统计中文字符
print("该字符串有数字{0}个".format(count3)) # 统计数字字符
print("该字符串有其他字符{0}个".format(len(str)-count1-count2-count3-str.count(" "))) # 统计其他字符
浙公网安备 33010602011771号