Eduardo Vasquez
Eduardo Vasquez

Reputation: 29

Count specific character in text file

How would I count the number of times a specific character (nonwhitespace) occurs in a text file? (i.e "," "." "a" "k" "m")

Here is what I have so far:

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: ")

code for distribution so far

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)

This gives me my desired output for the a and b but is not very efficient to write each line of code for every character. How would I loop through for every possible character and then print out the count?

Upvotes: 0

Views: 2494

Answers (1)

falsetru
falsetru

Reputation: 368984

Use collections.Counter.

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))

Upvotes: 1

Related Questions