编写程序实现对特定英文文章(文本文件)的单词数和有效行数的统计,其中要求空行不计数;
def getText(): txt=open("english.txt","r").read() txt=txt.lower() for ch in '~!@#$%^&*()_+"{}[]|?.<>?': txt=txt.replace(ch,"") return txt hamletTxt=getText() words=hamletTxt.split() counts={} for word in words: counts[word]=counts.get(word,0)+1 items=list(counts.items()) print(len(items)) non_blank_count = 0 with open('english.txt') as infp: for line in infp: if line.strip(): non_blank_count += 1 print(non_blank_count)