irakli
irakli

Reputation: 51

Print statistics for every character in a file in python

What I am trying to do is to take a file's data and print out the percentage of each character in file but I don't want to use duplicates. I need to print just one character with a relevant percentage point. Below is the snippet.

for all_char in text:
    char_counter = 0 
    if count_char(text, all_char) > 1:
        perc1 = 100 / len(text) * count_char(text, all_char)
        print("{0} - {1}%".format(all_char, round(perc1, 2)))
        with open(filename, "w") as w:        #<-------- I need a code to remove a single character
            w.truncate(char_counter)
            char_counter += 1

    elif count_char(text, all_char) == 1:
        perc2 = 100 * count_char(text, all_char) / len(text)
        print("{0} - {1}%".format(all_char, round(perc2, 2)))
        char_counter += 1

Above I made a variable called char_counter which will be increased after every iteration and the function called count_char will tell, how many times each character is used in file and if that number is higher than 1 the character must be deleted from the file means that it will print only once. This is the base idea but the code gives me an error.

Upvotes: 1

Views: 758

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 61042

You can get the character counts of the entire file by using a Counter over the characters. Then the percentage of each character is count for that character/total count.

from collections import Counter
from itertools import chain

with open(filename) as f:
    counts = Counter(chain.from_iterable(f))

total = sum(counts.values())

for character, count in counts.items():
    print('{:<2} - {:>6.2f}%'.format(repr(character)[1:-1], (count/total) * 100))

For the text

Mary had a little lamb.

This prints

M  -   4.17%
a  -  16.67%
r  -   4.17%
y  -   4.17%
   -  16.67%
h  -   4.17%
d  -   4.17%
l  -  12.50%
i  -   4.17%
t  -   8.33%
e  -   4.17%
m  -   4.17%
b  -   4.17%
.  -   4.17%
\n -   4.17%

Upvotes: 2

Related Questions