Python counts the number of English characters

When doing a big English homework, the teacher asked to write at least 5000 characters, but Word can only count the number of words, so I will write a code to help me calculate it.

Article directory


the code

# -*- coding: utf-8 -*-
#首先将你需要计数的文本复制到一个.txt文件中去,然后使用这个函数读取计算
def read_word(filename):
#传入文件名称
    f = open(filename+".txt", "r", encoding="utf-8")
    txt = f.read()#读取文本信息
    f.close()#关闭文件
    number = 0#用于统计字符数
    for i in txt:#遍历每个字符
        if i>="a" and i<="z" or i >="A" and i <="Z":#如果是英文字符
            number =number+1   #数量加一
            print(i,end='')#用于显示纯英文字母文本信息
        if i=="\n":
            print(" ")
        if i ==" ":
            print(" ",end='')
    print("\n总共有"+str(number)+"个字符")


if __name__ == '__main__':
    while(True):#使用循环能够多次计数
        filename = input("提示:请将要统计的英文文献复制到一个.txt文件中用于统计\n本程序将只统计英文字符个数,不包含括号等其他符号\n请输入文件名:")
        read_word(filename)
        continue_=input("是否继续统计?Y 、N:")
        if continue_ =='N':
            break




Summarize

This is a super simple counting tool

Guess you like

Origin blog.csdn.net/CBCY_csdn/article/details/126959258