我如何计算特定字符(非空白)在文本文件中出现的次数? (即“,”“。”“a”“k”“m”)
这是我到目前为止所做的:
file = open("filename.txt","r")
num_char = 0
num_words = 0
num_lines = 0
for line in file:
words = line.split()
num_lines += 1
num_words += len(words)
num_char += len(line)
print ("Character count:\t" + str(num_char))
print ("Word count:\t\t" + str(num_words))
print ("Line count:\t\t" + str(num_lines))
print ("Distribution of characters: ")
到目前为止分发的text = file.read()
file.close()
words = text.strip()
final = words.lower()
for i in range(len(words)):
first = final.count("a")
second = final.count("b")
print (first)
print (second)
这给了我所需的a和b输出,但是为每个字符编写每行代码效率不高。我将如何遍历每个可能的角色,然后打印出计数?
答案 0 :(得分:1)
from collections import Counter
file = open("filename.txt", "r")
num_char = 0
num_words = 0
num_lines = 0
char_distribution = Counter()
for line in file:
words = line.split()
num_lines += 1
num_words += len(words)
num_char += len(line)
char_distribution += Counter(line.lower())
print("Character count:\t{}".format(num_char))
print("Word count:\t\t{}".format(num_words))
print("Line count:\t\t{}".format(num_lines))
print("Distribution of characters: ")
for char, count in sorted(char_distribution.items()):
if char.isalpha() or char in ',.':
print("\t{}\t\t{}".format(char, count))